3. Introduction to Control Structure
Control Structure are used to control the
flow of program’s execution. By default control
structure in a program are executed sequentially.
There are two types of control structure:-
Decision-making Statement
LOOPING STATEMENT0
4. There are four types of decision making
statement:-
(1) Simple if statement
(2) If-else statement
(3) Nested if statement
(4) Select case statement
5. SIMPLE IF STATEMENT
There is only one condition in the program, then
simple if statement is used.It has only the true block
of the statement.
Syntax:-
If(Condition) then
Statement block
End If
Example:-
If(average>40) then
MsgBox”Passed”
End If
6. The if else statement works like that of simple if statement. With the
difference that if you want to execute the false block differently then
if else statement is used.In this type of statement you can have one
block corresponding to the true condition and the other block
corresponding to the false condition.
Syntex:-
if(Condition) then
Statement block-1
Else
Statement block-2
End If
Example:-
If(average>40) then
MsgBox “Passed’
Else
MsgBox “Failed”
End If
7. NESTED IF STATEMENT
If you have more then two condition to be tested then another if statement is used in else part of the if
statement it is called nesting.
Syntex;-
If(Condition-1) then
Statement block-1
Else
If(Condition-2) then
Statement block-2
Else
If(Condition-3) then
Statement block-3
-
-
Else
Satement block-n
End If
End If
End If
END SUB
8. Dim average As Integer
average = Val(Text1.Text)
If (average > 75) Then
printf = "A"
Else
If (average > 65) Then
printf = "B"
Else
If (average > 55) Then
printf = "C"
Else
If (average > 45) Then
printf = "S"
Else
printf = "F"
End If
End If
End If
End If
End Sub
Example:-
9. SELECT CASE STATEMENT
The select case statement is used as an alternative of if then else for selectively
Executing one block of statement from among multiple block of statement.
Syntex:-
Select case Expression
Case Expression-1
Statement block-1
Case Expression-2
Statement block-2
-
-
-
Case Else
Statement block-n
End select
Conti....2...
10. Example:-
days = Val(Text1.text)
If Val(Text1.text) = 1 Then
MsgBox "Sunday"
Else
If Val(Text1.text) = 2 Then
MsgBox "Monday"
Else
If Val(Text1.text) = 3 Then
MsgBox "Tuesday"
Else
If Val(Text1.text) = 4 Then
MsgBox "Wednesday"
Else
If Val(Text1.text) = 5 Then
MsgBox "Thursday"
Else
If Val(Text1.text) = 6 Then
MsgBox "Friday"
Else
If Val(Text1.text) = 7 Then
MsgBox "Saturday"
Else
MsgBox "not weakday"
End If
End If
End If
End If
End If
End If
11. Looping Satetment
There are six type of looping statement in visual basic:-
(1)While wend loop
(2)Do while loop
(3)Do untill loop
(4)Do loop until
(5)For next loop
(6)For each... next loop
12. While wend loop:-
While wend loop execute a block of statement while condition is
true.
Syntex:-
While(Condition) then
Statement block
Wend
Example:-
Dim num as Integer
Num=0
While(num<=10)
Print num
Num=num+1
Wend
13. DO WHILE LOOP
Do while loop is exit control loop. Do while works with
comparison expression just as the if statement does.
Syntex:-
Do while(Condition) then
Statement block
Loop
Example:-
Dim i As Integer
i=0
Do while(i<10)
Print i
i=i+1
loop
14. DO UNTIL LOOP
Do while loop continues executing the body of the loop as long as
the comparision test is true.But do until loop executes the body
of the loop as long as the comparision test is false.
Syntex:-
Do until (Condition)
Statement block
Loop
Example:-
Dim i As Integer
i=0
Do until (i>10)
Print i
i=i+1
loop
15. The loop continues until the loop until statement. The comparision test
appears at the bottom of the loop. Hence, the body of the loop always
executes atleast once.The body of the loop executes more than once as long
as the comparision test stayes false.
Syntex:-
Do
Statement block
Loop until (Condition)
Example:-
Dim i As Integer
i=0
Do
Print i
i=i+1
loop until (i>10)
16. The for loop repeats for a specific number of
times.Hence,for is a Keyword, counter is a variable name &
value1,value2 are integer values. The block of statement
should be a valid VB statement.
Syntex:-
For counter=value1 To value2
Statement block
Next Counter
Example:-
For i=1 to 10
Print i
Next i
17. FOR EACH.....NEXT LOOP
For each...next loop repeats a group of statement for each
element in a collection of objects or in an array instead of
repeating the statement a specified number of time.(Collection
can be of objects or arrays)
Syntex:-
For Each object/array
VB statement
Next
Example:-
For each element in group
VB statement
Next element