week 4

System

Select Statements

A selection statemetn is a type of conditional statement, these allow the the programmers to make a choice/selection about which lines of code to run. Within VB .NET® a number of selection types are available the commonly used are ‘If…then…else’ and ‘Select…Case’.

The ‘If...then…else’ statement

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System. EventArgs) Handles TextBox1.Leave If Not IsDate(TextBox1.Text) Then MsgBox("Please enter date (dd/mm/yy)", MsgBoxStyle.Information) TextBox1.Focus() Else MsgBox("Date acceptable", MsgBoxStyle.Information) End If End Sub
Understanding the above code, the first TextBox (TextBox1) will allow the entry of a date in the format dd/mm/yy, e.g. 29/08/81 would be entered for 29 August 2081.
The second TextBox (TextBox2) will allow entry of a time.
If a user were to enter an invalid date (tested using the IsDate function in VB .NET®), the first part of the ‘If…then….else’ statement displays a message box and then user is returned to the Date TextBox (TextBox1) for another try.
There is a test for a valid date when the user moves away from the Date TextBox. This is performed by the ‘Leave’ event handler. But if the user enters a valid date, the ‘else’ part of the ‘If…then…else’ function is ran. This simply pops up a message box, but displays a confirmation that the date was acceptable.


select case

Select…Case statement

Case statements are usualy found in places where code is simplified to enable a program to match a single value (or range of values) from a list of given possibilities.
Example of the ‘Select…Case’ statement.

Select Case (Val(TextBox1.Text)) Case 1 MsgBox("Sorry, no discount!", MsgBoxStyle.Information) Case 2 To 5 MsgBox("10% discount.", MsgBoxStyle.Information) Case 6, 7, 8 MsgBox("20% discount.", MsgBoxStyle.Exclamation) Case Else MsgBox("Sorry, you can’t order more than 8.", MsgBoxStyle.Critical) TextBox1.Focus() End Select

loop

Loops

Loops allow a program to perform a number of actions repeatedly. Unless the loop is infinite, it will have an element or condition statement that will make it to stop.
Within the VB .NET framework there are a number of different loops that can be used these are,

For…Next
The ‘For…Next’ statement is one of the most commonly used tools in almost all programming languages. A loop uses a predefined number of times that is set by a programmer, that a piece of code is ran until a statement is met, usually controlled by a counter. The example below shows how an application uses a ‘For…Next’ loop to generate a child’s times table based on two inputs (the table number itself and the number of rows required).

Dim counter As Byte Dim newline As String TextBox3.Text = TextBox1.Text & " times table" & vbCrLf For counter = 0 To Val(TextBox2.Text) newline = counter & " X " & TextBox1.Text & " = " & _ Val(TextBox1.Text) * counter & vbCrLf TextBox3.AppendText(newline) Next counter

Do…loop

The ‘Do…’ loop can either check a condition before- or after the lines of code that are being repeated. The example below a vowel counter uses the Do…’ loop. The event handler is triggered when a new keystroke is made. The application then runs a check after the condition ‘Do…’ loop which repeats until the counter is greater than the length of the current text stored in TextBox1. Inside the loop, a ‘Select…Case’ is used to examine each character (by using the MIDfunction) and increment the appropriate counter when a vowel is matched. Dim counter As Byte Dim nextchar As Char Dim acount As Byte Dim ecount As Byte Dim icount As Byte Dim ocount As Byte Dim ucount As Byte counter = 1 Do nextchar = Mid(TextBox1.Text, counter, 1) Select Case (nextchar) Case "a", "A" acount = acount + 1 TextBox2.Text = acount Case "e", "E" ecount = ecount + 1 TextBox3.Text = ecount Case "i", "I" icount = icount + 1 TextBox4.Text = icount Case "o", "O" ocount = ocount + 1 TextBox5.Text = ocount Case "u", "U" ucount = ucount + 1 TextBox6.Text = ucount End Select counter = counter + 1 Loop Until counter > TextBox1.TextLength

While…End

This works in a similar fashion to the pre-check ‘Do...’ loop as shown above, so we won’t dwell too much on it here! counter = 0 While counter < 10 counter = counter + 1 MsgBox("Loop has run " & counter & " time(s)!") End While

Using the tutorial sheet above create the code to enable the use of one of the above covered loops
#
5 min's

How an operating system can be viewed as an event driven application

Event driven programs for non-graphical applications

Where might this happen? Well if we look closer you may find them in a number of places that you might never of considered, things like;

  1. Alarms
  2. Traffic light systemes
  3. Microwaves
  4. Washing Machines
  5. DOS (Disk Operating System) aka Command line (CLI)
  6. TV remote


Other places are discussed in the short video below.

#


1. Name three different event driven programming languages.
2. Common triggers can be caused by the ______or the ________? Complete this sentence.
3. Name three possible system events.
4. Give three advantages of event driven programming languages.
5. Give three disadvantages of event driven programming languages.


EXTENTION

EXTENTION

STRETCH & CHALLENGE


The information covered in this page covers the knowledge and understanding linked to the criterion in the table below.

Links to Assessment Criteria Description
LO1 Understand the features of event driven programming:
Key features: service oriented; time driven; event handlers; trigger functions; events eg mouse, keyboard, HTML object, form, user interface; event loops; flexibility; suitability for graphical interfaces; simplicity of programming; ease of development
LO2 Be able to use the tools and techniques of an event driven language:
Triggers: eg key press, alarm, system event, touch screen event, mouse click
Tools and techniques: eg use of tool boxes and controls, selection, loops, event handlers, triggers, objects and object properties, menus; debugging tools
Variables: declaring variables; scope of variables; constants; data types
Functional Skills English Reading, Writing, Comprhension, Problems Solving, Nouns
Functional Skills Maths Compression,Reduction
British Values None covered in this session.
Equality and Diversity

Last Updated
2018-04-16 13:27:00

Links to Learning Outcomes

Links to Assessment criteria

 


English

0

Maths







How 2's Coverage

0

0

Files that support this week


| | | | |
Week 3
Prev
Week 4
Prev
Week 5
Prev

Next

Next
Webmaster Spelling Notifications