VB Control Structures
VB Control Structures
Control Flow
In a program, statements may be executed
sequentially, selectively or iteratively. Every
programming language provides constructs
to support sequence, selection or iteration.
So there are three types of programming
constructs :
Sequential Constructs
Selection Constructs
Iterative Constructs
Sequential Construct
The
Statement 1
Statement 2
Statement 3
Selection Construct
Selection Construct
One course-of-action
Condition
?
true
Statement 1
false
Statement 1
Statement 2
Another
courseof-action
Statement 2
Iterative Constructs
The
Iterative Construct
false
The exit condition
Condition
?
True
Statement 1
Statement 2
The loop
body
Selection Constructs
VB provides two types of selection construct :
If statement
If..Then Statement
If..Then..Else Statement
If..Then..ElseIf Statement
Nested Ifs
If..Then Statement
Definition
If..Then Statement
Example 1
If (Num>0) Then
Print It is a positive number
End if
Example 2 :
If txtAge.Text>=18 Then
Print You are eligible to vote
End if
If..Then..Else Statement
If..Then..Else statement provides an alternate
choice to the user i.e. if the condition is true
then a set of statements are executed otherwise
another set of statements are executed.
Syntax :
If (Boolean Expression) Then
VB Statement(s)
Else
VB Statement(s)
End If
Examples of If..Then..Else
Example 1
If txtAge.Text>=18 Then
Print You are eligible to vote
Else
Print Sorry, You are not eligible to vote
End If
Example 2
If Num Mod 2=0 Then
Print It is an Even Number
Else
Print It is an Odd Number
End If
If..Then..ElseIf Statement
Example of If..Then..ElseIf
If (Age<=4) Then
Print Your rate is free.
ElseIf (Age<=12) Then
Print You qualify for the childrens rate.
ElseIf (Age<65) Then
Print You must pay full rate
Else
Print You qualify for the seniors rate.
End If
Nested Ifs
A
I
If (expresssion 1) Then
If (expression 2 ) Then
Statement 1
Else
Statement 2
End If
Else
body-of-else
End If
Nested Ifs
2. If (expression 1) Then
body-of-if
Else
:
If (expression 2) Then
Statement-1
[Else
Statement-2]
End If
Nested Ifs
3) If (expression 1) Then
:
If (expression 2) Then
Statement-1
[Else
Statement-2]
End If
Else
If (expression 3) Then
Statement-3
[Else
Statement-4]
:
End If
End If
Select-Case Statement
Select-Case
Example of Form 1
Select Case byMonth
Case 1,3,5,7,8,10,12
number_of_days=31
Case 2
number_of_days=28
Case 4,6,9,11
number_of_days=30
End Select
Syntax of Form 2
Select Case : Second Form [Relational Test]
Select Case Expression
Case is relation :
one or more visual basic statements
Case is relation :
one or more visual basic statements
[Case Else :
one or more visual basic statements
End Select
Example of Form 2
Select Case marks
Case Is < 50
Result = Fail
Case Is < 60
Result = Grade B
Case Is < 75
Result = Grade A
Case Else
Result = Grade A+
End Select
Example of Form 3
Select Case Age
Case 2 to 4 : Print PreNursery
Case 4 to 6 : Print Kindergarden
Case 6 to 10 : Print Primary
Case Else : Print Others
End Select
Home Assignment 1
Write
Home Assignment
Write
1)
2)
Looping Structures
1.
2.
3.
For..Next Statement
This
Syntax
Examples
Example
More Examples
Example
More Examples
Example
5 : Find factorial of a
given number N.
:
Fact=1
For I= 1 to N
Fact = Fact * I
Next I
Print Factorial of ; N; =; Fact
:
Home Assignment 2
1)
2)
3)
4)
Do..Loop Structures
Do
Examples of Do While..Loop
Example
Do..Loop While
Do
Examples
Example
1:
Do
num = InputBox (Enter a
number)
sum = sum + num
Loop While num < > 0
Here the statements inside the loop
will be executed once no matter
what the comparison test evaluates
to.
Do..Until Loop
Do
Examples of Do Until..Loop
Example
Do..Loop Until
Do
Examples
Example
1:
Do
num = InputBox (Enter a
number)
sum = sum + num
Loop Until num = 0
Here the statements inside the loop
will be executed once no matter
what the comparison test evaluates
to.
While..Wend
While..Wend
loop is functionally
equivalent to the Do While..Loop.
It executes a set of VB statements
till the condition evaluates to true.
Syntax
:
While <Condition>
one or more vb statements
Wend
Examples
Example
Nested Loops
A
Examples
Example
1
2
3
4
5
2
33
444
5555
Sol
:
For I = 1 To 5
For J = 1 To I
Print I;
Next J
Print
Next I
1)
3)
Home Assignment - 5
Programming
Questions : Develop
programs for the followings :
To generate the mirror image of the
number entered by the user.
To generate the prime numbers
between 100 and 700.
To Check whether the number entered
by the user is a prime number or not.
To print first 30 multiples of the number
entered by the user.
To generate the factorial of the number
input by the user.
Home Assignment - 5
To
Assessment Tools
Students