Visual Basic (2008 Express) | ||
Constructs | ||
In VB the instructions are executed in the order they are
written, but there are exceptions...
|
||
Selection : The syntax for this is...
|
||
If (boolean
expression) Then statement 1 statement 2 statement 3 Else statement 4 statement 5 End if
|
A boolean expression is a
statement that can either be TRUE or FALSE. Eg. (Name="Tom") or (Mark > 50) |
|
The statements 1, 2 and 3 will only be executed if the
boolean expression is TRUE. If it is FALSE, then statements 4 and 5 will
be executed.
The Else section may be left out.
|
||
Example : An exam mark is
input. If the mark is 50 or more then the message "PASS" is
output. Otherwise the message "FAIL" is output. |
||
Dim
ExamMark As Integer
ExamMark = InputBox( "Please enter the mark") If (ExamMark >= 50) ThenMsgBox("Pass") Else MsgBox("Fail") End If
|
||
Repetition
If the number of times a loop is to be executed is known use a For...Next Loop. Example : Add each of 10 Marks stored in an array to a ListBox...
|
||
Dim
Mark(9) As Integer
Mark(0) = 65 ListBox1.Items.Add(Mark(i)) Next
|
![]() The resulting ListBox is shown here. |
|
Loops that are repeated until a condition is met are
programmed using either...
Do...Loop Until (condition) or Do While (condition)...Loop
|
The condition is a boolean expression, that is either TRUE or FALSE. | |
Example : A sequence of exam
marks is to be input, until the number 0 is input.
|
||
Do
Mark = InputBox( "Please enter mark") Loop Until (Mark = 0)
|
||
Back |