Professional Documents
Culture Documents
IMT309 Control Structures
IMT309 Control Structures
MODULE THREE
on
VB.NET Control
Structures
Prepared By : D H Usman
Module Summary
This module captioned “Control Structure” is mainly
focused on the working principle of various control
Structures in VB and their implementation in
Programming.
Various Practical Examples have been included in
this module relating to real life problems for the
better understanding of the concepts.
Programming questions, Output related questions are
formed so as to improve the logical & problem
solving ability of the students.
Prepared by: D H Usman 2
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
Prepared by: D H Usman 3
Sequential Construct
The sequential construct means the statements
are being executed sequentially. This
represents the default flow of statements.
Stament 1
Stament 2
Stament 3
Prepared by: D H Usman 4
Selection Construct
The selection construct means the execution of
statement(s) depending upon the condition-
test. If a condition evaluates to true, a course-
of-action (a set of statements) is followed
otherwise another course-of-action is
followed. This construct is also called decision
construct as it helps in decision making.
false
Statement 1 Another
course-
of-action
Statement 2
false
Condition
?
The exit condition
True
Statement 2
Example 2 :
If txtAge.Text>=18 Then
Print “You are eligible to vote”
End if
Prepared by: D H Usman 11
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 :
Example 2 :
If Num Mod 2=0 Then
Print “It is an Even Number”
Else
Print “It is an Odd Number”
Prepared by: D H Usman 13
End If
If..Then..ElseIf Statement
If..Then..ElseIf statement is used to test a number of mutually
exclusive cases and only executes one set of statements for the
case that is true first.
Syntax :
Syntax :
For <counter Variable>=<start_val> To <end_val> Step
<increment/Decrement Value>
‘ One or more VB Statements
Next <counter Variable>
Prepared by: D H Usman 24
Examples
Example 1 : Generate natural no’s from 1 to
100
For I = 1 To 100
Print I
Next I
Example 2 : Generate first 20 even no’s.
For E = 2 to 40 Step 2
Print E
Next E
Prepared by: D H Usman 25
Do..Loop Structures
Do While..Loop : Do While loop is an entry
controlled loop in which the condition is
placed at the entry point. This statement
executes the statements specified in the body
of the loop till the condition evaluates to true.
The loop may not be executed at all the if the
condition is initially false.
Syntax :
Dim P as Byte
P=20
Do While P>5
P=P-2
Loop
Prepared by: D H Usman 27
Do..Loop While
Do Loop While is an exit controlled loop as
the condition is placed at exit point. The body
of the loop is going to be executed at least
once whether the condition evaluates to true or
false. Loop is executed as long as the result of
the condition remains true.
Syntax :
Do
One or more VB Statements
Loop While <condition or Boolean Expression>28
Prepared by: D H Usman
Examples
Example 1 :
Do
num = InputBox (“Enter a number”)
sum = sum + num
Loop While num < > 0
Here the statements inside the loop will be
Dim P as Byte
P=20
Do Until P<5
P=P-2
Loop
Prepared by: D H Usman 31
Do..Loop Until
Do Loop Until is an exit controlled loop as the
condition is placed at exit point. The body of
the loop is going to be executed at least once
whether the condition evaluates to true or
false. Loop is executed as long as the result of
the condition remains false.
Syntax :
Do
One or more VB Statements
Loop Until <condition or Boolean Expression> 32
Prepared by: D H Usman
Examples
Example 1 :
Do
num = InputBox (“Enter a number”)
sum = sum + num
Loop Until num = 0
Here the statements inside the loop will be
Syntax :
While <Condition>
one or more vb statements
Wend
Prepared by: D H Usman 34
Examples
Example 1 : Generate the sum of first 10
natural no’s
I=1
While I<=10
Sum = Sum + I
I=I+1
Wend
Print “Sum of 10 natural no’s = ” ; Sum
Prepared by: D H Usman 35
Nested Loops
A loop within another loop is called as Nested
Loop.
Example :
For I = 1 to 5
For J = 1 To 3
Print J Inner Loop Outer Loop
Next J
Print
Next I
Prepared by: D H Usman 36
Working with Nested Loops
In nested loops the inner loop is executed
completely for one time execution of the outer
loop. In the Previous example the Inner Loop
will be executed three times for every
execution of the outer loop.
Nested loops are very useful when there is a
requirement to generate different kind of
patterns as output.