LOGO
Control Structures
In Visual Basic
Submitted By:
SakarChiolunkar12C3045
ShashankBaghel12C3053
Tusharjain 12C3062
Company Logo
Control Statement
The order in which statements are executed in a program is
called the flow of control. In a sense, the computer is under the
control of one statement at a time. When a statement has been
executed, control is turned over to the next statement (like a
baton being passed in a relay race). Flow of control is normally
sequential. That is, when one statement is finished executing,
control passes to the next statement in the pro-gram. If we
want the flow of control to be nonsequential, you can use
control structure.
Company Logo
Control Flow
Sequence
Functions and Procedures
Selection
If...Then...Else statement
Select Case statement
Iterative
For...Next Loop statement
Do...Loop statement
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 Construct
The sequential construct means the statements are being
executed sequentially. This represents the default flow of
statements.
Statement 1
Statement 2
Statement 3
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.
Condition
?
Statement 1 Statement 2
Statement 1
Statement 2
Another course
of action
true
One course-of-action
false
If...Then...Else statement
Perhaps the most important statement in a program is the If statement and then its
statements. In other words
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.
In Visual Basic we use three types of ‘IF’ statements:
1.Simple If
2.If Else
3.Nested If
Syntax :
If (booleanExpression) Then
VB Statement(s)
Else
VB Statement(s)
End If
If...Then...Else statement
Condition
?
Statement Statement
true false
Select Case statement
If we have a lot of conditional statements, using If..Then..Elsecould be very
messy. For multiple conditional statements, it is better to use Select Case or
Select Case allows multi way branching through the code.
Syntax :
Select Case expression ‘expression maybe string or numeric
Case value1
Block of one or more VB statements
Case value2
Block of one or more VB Statements
Case value3
Block of one or more VB statements
Case value4
.
.
.
Case Else
Block of one or more VB Statements
End Select
Select Case statement
Case 1
Case 2
Case N
Case 1 Statements
Case 2 Statements
Case N Statements
Case Else Statements
true
true
true
false
false
Iterative Constructs
The iterative or repetitive constructs means repetition of a set-of-statements
depending upon a condition-test. A set-of-statements are repeated again and
again till the condition or Boolean Expression evaluates to true. The iteration
constructs are also called as looping constructs.
Condition
?
Statement 2
Statement 1
The loop
body
false
The exit condition
True
For...Next Loop statement
Repeats a group of statements a specified number of times.
Syntax :
For counter [ As datatype] = start To end [ Step step]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
Do...Loop statement
Repeats a block of statements while a Boolean condition is True or
until the condition becomes True.
Syntax :
Do { While | Until } condition
[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop
-or-
Do
[ statements ]
[ Continue Do ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition