Conditional Statements and Loops in Visual Basic
Conditional Statements and Loops in Visual Basic
Conditional Statements and Loops in Visual Basic
If...Then Statement:
Description:
The If...Then statement examines the truthfulness of an expression.
Syntax:
If ( expression ) Then
Statement
End If
The program examines a condition. This expression can be a simple expression or a
combination of expressions.
If the expression is true, then the program will execute the Statement.
There are two ways we can use the If...Then statement.
If the conditional formula is short enough, we can write it on one line, like this:
If there are many statements to execute as a truthful result of the condition, we should
write the statements on alternate lines.
Of course, you can use this technique even if the condition you are examining is short.
In this case, one very important rule to keep is to terminate the conditional statement
with End If.
If expression Then
Statement
End If
Here is another way:
If Condition Then
Statement1
Statement2
Statementn
End If
VB comparison operators:
< Less Than
<= Less Than or Equal to
> Greater Than
>= Greater Than or Equal to
= Equal to
<> Not Equal to
Example:
Private Sub Command1_Click()
Dim var1 As Integer
var1 = 10
If var1 = 10 Then
MsgBox "The condition is true."
End If
End Sub
If....Then....Else statement:
Description:
The If...Then statement offers only one alternative: to act if the condition is true.
Whenever we would like to apply an alternate expression in case the condition is false we
can use the If...Then...Else statement.
Syntax:
If ( expression ) Then
Statement1
Else
Statement2
End If
When this section of code is executed, if the expression is true, then the first statement,
Statement1, is executed.
If the expression is false, the second statement, Statement2, is executed.
Example:
Private Sub Command1_Click()
Dim var1 As Integer
var1 = 5
If var1 = 10 Then
MsgBox "The variable is ten"
Else
MsgBox "The variable is not ten"
End If
End Sub
The expression is evaluated. If the resulting value is non-zero, the statement(s) after the
Then are executed.
If there is an Else and if the expression evaluated to zero, the statement(s) after the Else
are executed.
Nested If statement:
Description:
The If...Then...ElseIf statement acts like the If...Then...Else expression, except that it
offers as many choices as necessary.
Example:
Select statement:
Description:
If we have a large number of conditions to examine, the If...Then...Else will go through
each one of them.
Visual Basic offers the alternative of jumping to the statement that applies to the state of
the condition.
Syntax:
The formula of the Select Case is:
If there could be no match between the Expression and one of the Expressions,use a Case
Else statement at the end of the list.
The statement would then look like this:
Example:
Private Sub Form_Click()
Select Case BackColor
Case vbRed
BackColor = vbBlue
Case vbBlue
BackColor = vbGreen
Case vbGreen
BackColor = vbBlack
Case Else
BackColor = vbRed
End Select
End Sub
IN-DETERMINANT LOOP IN VISUAL BASIC
Do While (Expression)
(Code to execute)
Loop
Example:
The following Do Loop counts from 1 to 100.
Syntax:
Do Until (Expression)
(Code to execute)
Loop
Numbers between 1 to 1000 will be displayed on the form as soon as we click on the
command button.
Syntax:
Example:
Syntax:
Do
(Code to execute)
Loop while (test condition)
Example:
Determinate Loops:
Description:
The For...Next Loop is another way to make loops in Visual Basic.
It comes under the category of determinant loop which means number of times loop
repeats is known.
Repeats a group of statements a specified number of times.
Syntax:
For counterVariable = fromValue To toValue step stepvalue
...VB Statements...
Next counterVariable
Example:
The following loop counts the numbers from 1 to 100:
Dim x As Integer
For x = 1 To 50
Print x
Next
In order to count the numbers from 1 to 50 in steps of 2, the following loop can be used
For x = 1 To 50 Step 2
Print x
Next
The above coding will display numbers vertically on the form. In order to display numbers
horizontally the following method can be used.
For x = 1 To 50
Print x & Space$ (2);
Next
To increase the space between the numbers increase the value inside the brackets after
the & Space$.
Following example is a For...Next repetition structure which is with the If condition used.
In the output instead of number 4 you will get the "This is number 4".
It is often necessary to exit a For loop before it has completed the specified number
of loop iterations. This is achieved using the Visual Basic Exit For statement. This is
typically used in conjunction with a conditional If statement.
The Following code example causes the loop to exit if the counter is equal to 3:
For intCount = 0 To 5
If intCount = 3 Then Exit For
MessageBox.Show("Counter is currently: " + CStr(intCount))
Next intCount
Nested for:
A loop can be coded within another loop thus forming nested loops.
There can be many levels of nesting.
nesting is possible as long as the inner structure is completely enclosed in the outer
structure.
When nesting loops, each loop must have a unique counter variable.
For example, nested loops are needed to display a table of values, to process the
elements of a two-dimensional array, or to sort array elements using the Bubble Sort
algorithm
Syntax:
for counter-variable1=start-value to end-value[step step-value]
statements......
for counter-variable2=start-value to end-value[step step-value]
statements......
next counter-variable2
next counter-variable1
Example:
To print the elements of an 2x2 array
for i=1 to 2
for j=1 to 2
print a(i,j),
next j
print
next i