You are on page 1of 15

Unit II

Control Statements – Determinate


and Indeterminate Loops
Control Statements
• Control Statements (Control structures) are used to
control the flow of program's execution.
• Visual Basic supports control structures also called
as decision making statements such as
– if... Then,
– if...Then ...Else,
– Select...Case,
and Looping structures such as
- Do While...Loop,
- While...Wend,
- For...Next
If...Then Statement
• The If...Then statement performs an indicated
action only when the condition is True; otherwise
the action is skipped.
• Syntax of the If...Then selection
If <condition> Then
statement
End If

• e.g.: If a > b Then


Print “ a is greater”
End If
If...Then...Else statement
• The If...Then...Else statement allows the programmer to
specify that a different action is to be performed when
the condition is True than when the condition is False.
• Syntax of the If...Then...Else selection
If <condition > Then
statements
Else
statements
End If
• e.g.: If a > b Then
Print “A is greater”
Else
Print “ B is greater”
End If
Nested If...Then...Else statement
• Nested If...Then...Else statement test for multiple cases
by placing If...Then...Else statements inside another
If...Then...Else structures.
• Syntax of the Nested If...Then...Else selection structure
If < condition 1 > Then
statements
ElseIf < condition 2 > Then
statements
ElseIf < condition 3 > Then
statements
Else
Statements
End If
Nested If...Then...Else selection structure contd..
• Example coding to find the grade using nested if and
display in the form.

If average > 75 Then


Print “ Congratulations - your grade is A”
ElseIf average > 65 Then
Print “Your grade is B”
ElseIf average > 55 Then
Print “ Your grade is C”
ElseIf average > 45 Then
Print “Your grade is D”
Else
Print “ Your grade is F”
End If
Select...Case statement
• Select...Case structure is an alternative
to If...Then...ElseIf for selectively executing a single
block of statements from among multiple block of
statements.
• Select...case is more convenient to use than
the If...Else...End If.
• Syntax of the Select...Case selection structure
Select Case Index
Case 0
Statements
Case 1
Statements
End Select
Select...Case statement contd..
Example code to find the grade using select...case and display
in the form
Dim average as Integer
average = text5.text
Select Case average
Case 100 To 75
Print “ Congratulations - your grade is A”
Case 74 To 65
Print “Your grade is B”
Case 64 To 55
Print “ Your grade is C”
Case 54 To 45
Print “Your grade is D”
Case 44 To 0
Print “Your grade is F”
Case Else
Print “ Invalid mark entry”
End Select
Looping statements
• Visual Basic allows a procedure to be repeated
as many times as until given condition is true.
This is called Looping.
• Various looping structures are
- Do While...Loop,
- While...Wend,
- For...Next
Do While... Loop Statement
• The Do While...Loop is used to execute statements until a certain
condition is met.
• Syntax
Do While <condition>
Block of one or more VB statements
Loop
• Example code using Do Loop that prints from 1 to 100.
Dim number As Integer
number = 1
Do While number <= 100
number = number + 1
Print number
Loop
• A variable number is initialized to 1 and then the Do While Loop
starts. First, the condition is tested; if condition is True, then the
statements are executed. When it gets to the Loop it goes back to
the Do and tests condition again. If condition is False on the first
pass, the statements are never executed.
Do...Loop While Statement
• The Do...Loop While statement first executes the
statements and then test the condition after each
execution.
Example
Dim number As integer
number = 0
Do
number = number + 1
Print number
Loop While number < 100
• The programs executes the statements between Do
and Loop While structure in any case. Then it
determines whether the counter is less than 100. If so,
the program again executes the statements between
Do and Loop While else exits the Loop.
Do Until...Loop Statement
• Unlike the Do While...Loop , the Do Until...
Loop structure tests a condition for falsity.
Statements in the body of a Do Until...Loop are
executed repeatedly as long as the loop-
continuation test evaluates to False.
• An example for Do Until...Loop statement to print
the numbers from 1 to 1000.
Dim number As integer
number=0
Do Until number > 1000
number = number + 1
Print number
Loop
While... Wend Statement
• A While...Wend statement behaves like the Do
While...Loop statement.
Exa
The following While...Wend statements Prints 1 to
100
Dim number As Integer
number = 1
While number <=100
number = number + 1
Wend
The For...Next Loop
• The For...Next Loop is another way to make loops
in Visual Basic. For...Next statement executes a
statement until the condition is false.

• Syntax
For counter=startNumber to endNumber (Step increment)
One or more VB statements
Next
The For...Next Loop Contd…
Example 1
• The following loop prints the numbers from 1 to 100:
Dim x As Integer
For x = 1 To 50
Print x
Next
Example 2
• The following loop prints the numbers from 1 to 50 in steps of 2.
Dim x as integer
For x = 1 To 50 Step 2
Print x
Next
Example 3
• The following loop prints the numbers from 1000 to 5 in steps of -5.
Dim counter as integer
For counter=1000 to 5 step -5
Print counter
next

You might also like