Upload
njk19
View
229
Download
2
Embed Size (px)
Citation preview
7/28/2019 Controll Structures
1/89
Control Structures
If left unchecked by control-flow statements,a program's logic will flow through statements fromleft to right, and top to bottom.
Control structures allow you to control the flow ofyour program's execution.
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
2/89
Control Structures
Selection-(Branching)
Repetition-(Looping)
Sequences
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
3/89
Selection (Branching)
IF
Select Case
Goto
Control Structures
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
4/89
Control Structures
Repetition
For Next
Do While
Do until
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
5/89
Control Structures
Branching statements are used to cause certainactions within a program if a certain condition ismet.
If Then statement
If (Balance Check) < 0 Then Print "You are overdrawn"
Syntax
-Selection
IF Statement
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
6/89
Control Structures -Selection
Private Sub Command1_Click()
Dim Number As Integer
Number = InputBox("Enter value")
If Number > 50 Then MsgBox("Pass")
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
7/89
Control Structures
If Then and End If blocks allows multiplestatements:
If Then and End If blocks
IF thenstatement 1
statement 2
................
statement n
end if
Syntax
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
8/89
If Then end if
Private Sub Command1_Click()number = Text1.TextIf number Mod 2 = 0 Then
MsgBox ("Even")End If
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
9/89
Private Sub Command1_Click()
Dim balance As Currency
Dim check As Currency
balance = 5490.98
check = 9678.78
If balance - check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
End If
End Sub
If Then end if
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
10/89
If Then else end if
Here, the same two lines are printed if you areoverdrawn (Balance - Check < 0), but, if you are not
overdrawn (Else), your new Balance is
computed.
If Balance - Check < 0 ThenPrint "You are overdrawn"
Print "Authorities have been notified"
ElseBalance = Balance - Check
End If
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
11/89
If Then else end if
Write a program to read marks and display
Grade as Follows
Marks>=75 Grade is A
Marks=65 Grade is B
Marks =55 Grade is C
Other wise Grade is D
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
12/89
Private Sub Command1_Click()
Dim Marks As IntegerDim Grade As StringMarks = InputBox("Enter marks")If Marks >= 75 Then
Grade = "A"ElseIf Marks >= 65 Then
Grade = "B"ElseIf Marks >= 55 Then
Grade = "C"ElseIf Marks >= 0 Then
Grade = "D"
Else MsgBox ("Invalid marks")End IfMsgBox (Grade)
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
13/89
Select Case testexpression
[Case expressionlist1
[statementblock-1]]
[Case expressionlist2
[statementblock-2]].
.
.
[Case Else
[statementblock-n]]
End Select
Select Case
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
14/89
Select Case
Visual Basic provides the Select Case structure as
an alternative to If...Then...Else for selectively
executing one block of statements from amongmultiple blocks of statements. A Select Case
statement provides capability similar to the
If...Then...Else statement, but it makes codes more
readable when there are several choices.
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
15/89
Select Case
Write a program to read a number and display whether odd or
even using select case
Private Sub Command1_Click()
Dim number As Integer
number = InputBox("Enter integer")
Select Case number Mod 2
Case 0
MsgBox ("Even number")
Case 1MsgBox ("Odd Number")
End Select
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
16/89
Select Case
Write a program to read marks and display
Grade as Follows
Marks>=75 Grade is A
Marks=65 Grade is B
Marks =55 Grade is C
Other wise Grade is D
IT Department - National Institute of Education
End Show
7/28/2019 Controll Structures
17/89
Select Case
Private Sub Command1_Click()
Dim Marks As IntegerMarks = InputBox("Enter integer")
Select Case Marks
Case 75 To 100
MsgBox ("A")
Case 65 To 74MsgBox ("B")
Case 55 To 64
MsgBox ("C")
Case 0 To 54
MsgBox ("D")Case Else
MsgBox ("Invalid Marks")
End Select
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
18/89
.
The GoTo Statement
Another branching statement.
The format is GoTo Label, whereLabel is a labeled line.
Labeled lines are formed by typing the
Label followed by a colon.
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
19/89
Line10:
..Goto Line10
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
20/89
Private Sub Command1_Click()
Dim number As Integer
disp1: number = InputBox("Enter Number between 0 to 100")
If number
7/28/2019 Controll Structures
21/89
Private Sub Command1_Click()
Print "Sri Lanka"
Print "Sri Lanka"
Print "Sri Lanka"
Print "Sri Lanka"
Print "Sri Lanka"Print "Sri Lanka"
Print "Sri Lanka"
Print "Sri Lanka"
Print "Sri Lanka"
Print "Sri Lanka"
End Sub
For...Next
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
22/89
When you know you must execute
statements a specific number of times,
ForNext loop is the better choice.
For...Next
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
23/89
For loop uses a variable called a
counterthat increases or decreases
in value during each repetition of the
loop.
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
24/89
Forcounter= startToend[Step increment]
statement
Statement
statement
.
Next [counter]
The syntax is:
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
25/89
The arguments counter, start, end, and incrementare
all numeric.
The incrementargument can be either positive or
negative.
startmust be less than or
equal to endor the statements in the
loop will not execute.
Ifincrementis positive,
For i=1 to 100 step 5
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
26/89
startmust be greater than or
equal to endfor the body of the loop to execute.
If Step isn't set, then incrementdefaults to 1.
In executing the For loop, Visual Basic:Sets counterequal to start.
Ifincrementis negative
For i=100 to 1 step -5
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
27/89
In executing the For loop, Visual Basic:
1. Sets counterequal to start.
2. Tests to see ifcounteris greater than end.
If this is so, Visual Basic exits the loop.
5. Repeats steps 2 through 4.
3. Executes the statements.
4. Increments counterby 1 or by increment, if it's
specified.
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
28/89
FORcounter
=start
TOend
[STEPincrement
]statement
Statement
statementNEXT [counter]
1 2
3
4
Set counterequal to startTest to see ifcounteris greater than end.
Executes the statements.
Increments counterby 1 or by increment, if it's specified.
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
29/89
FORcounter
=start
TOend
[STEPincrement
]statement
Statement
statementNEXT [counter]
1 2
3
4
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
30/89
FOR counter= startTO end[STEP increment]
statement
Statement
statementNEXT [counter]
1 2
3
4
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
31/89
FOR counter= startTO end[STEP increment]
statement
Statement
statementNEXT [counter]
1 2
3
4
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
32/89
FOR counter= startTO end[STEP increment]
statement
Statement
statementNEXT [counter]
1 2
3
4
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
33/89
FOR counter= startTO end[STEP increment]
statement
Statement
statementNEXT [counter]
1 2
3
4
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
34/89
FOR counter= 1 TO 5
PRINT COUNTER
NEXTcounter
1 2
3
4
Sets counterequal to 1
Tests to see ifcounteris greater than 5
1
Increments counterby 1 or by increment, if it's specified.
Cont
IT Department - National Institute of Education End Show
C t
7/28/2019 Controll Structures
35/89
Write a program to Sri Lanka Five times
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 10
Print "Sri Lanka"
Next
End Sub
Cont
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
36/89
2. Write a program to read 15 numbers and display
the average.
1. Write a program to read ten numbers and display the total;
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
37/89
Write a program to Display the Following numbers
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
38/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
39/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
0 0
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
40/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 0
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
41/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 0i
7/28/2019 Controll Structures
42/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 0i
7/28/2019 Controll Structures
43/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 1
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
44/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 1
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
45/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 1
j
7/28/2019 Controll Structures
46/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i,j
Next j
Next i
End Sub
i j
1 1
j
7/28/2019 Controll Structures
47/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 1
1 1
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
48/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 1
1 1
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
49/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 2
1 1
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
50/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 2
1 1
J
7/28/2019 Controll Structures
51/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 2
1 1
J
7/28/2019 Controll Structures
52/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 2
1 11 2
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
53/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 2
1 11 2
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
54/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 3
1 11 2
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
55/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 3
1 11 2
J
7/28/2019 Controll Structures
56/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 3
1 11 2
J
7/28/2019 Controll Structures
57/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 3
1 11 21 3
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
58/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 3
1 11 21 3
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
59/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 4
1 11 21 3
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
60/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 4
1 11 21 3
J
7/28/2019 Controll Structures
61/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 4
1 11 21 3
J
7/28/2019 Controll Structures
62/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 4
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
63/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 5
1 11 21 31 4
IT Department - National Institute of EducationIT Department - National Institute of Education End Show
7/28/2019 Controll Structures
64/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 5
1 11 21 31 4
J
7/28/2019 Controll Structures
65/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 5
1 11 21 31 4
J
7/28/2019 Controll Structures
66/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 5
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
67/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 5
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
68/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
1 5
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
69/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 5
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
70/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 5
1 11 21 31 4
i
7/28/2019 Controll Structures
71/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 5
1 11 21 31 4
i
7/28/2019 Controll Structures
72/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 1
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
73/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 1
1 11 21 31 4
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
74/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 1
1 11 21 31 4
J
7/28/2019 Controll Structures
75/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 1
1 11 21 31 4
J
7/28/2019 Controll Structures
76/89
Private Sub Command1_Click()
Dim i,j as Integer
For i = 1 To 3
For j = 1 To 4Print i ,j
Next j
Next i
End Sub
i j
2 1
1 11 21 31 4
J
7/28/2019 Controll Structures
77/89
Write a program to read 6 number and
Display totalPrivate Sub Command1_Click()
Dim i As Integer
Dim number As Integer
Dim tot As Integer
For i = 1 To 6
number = InputBox("Enter number")
tot = tot + number
Next
MsgBox (tot)
End Sub
IT Department - National Institute of EducationIT Department - National Institute of Education End Show
7/28/2019 Controll Structures
78/89
2. Write a program to read 15 numbers and
display the average.
1. Write a program to read ten numbers and
display the total.
IT Department - National Institute of EducationIT Department - National Institute of Education End Show
Do while
7/28/2019 Controll Structures
79/89
Do while
Do...Loop, the statements execute as long as
the condition is True:
Do...Loop
Use a Do loop to execute a block of statementsan indefinite number of times.
There are several variations of the
Do...Loop statement,
but each evaluates a numeric condition
to determine whether to continue execution.
IT Department - National Institute of EducationIT Department - National Institute of Education End Show
Do while
7/28/2019 Controll Structures
80/89
Do While conditionstatements
Loop
When Visual Basic executes this Do loop,it first tests condition.
If it's True (nonzero),
Visual Basic executes the statements and then goes
back to the Do While statement and
tests the condition again.
Do while
IT Department - National Institute of EducationIT Department - National Institute of Education End Show
Write a program to Display Sri lanka 10 times
7/28/2019 Controll Structures
81/89
Write a program to Display Sri lanka 10 times
Private Sub Command1_Click()
Dim i As Integer
Do While i < 10
Print "Sri Lanka"
i = i + 1
Loop
End Sub
IT Department - National Institute of EducationIT Department - National Institute of Education End Show
Do while
7/28/2019 Controll Structures
82/89
Ifcondition is False (zero),
it skips past all the statements.
Do while
Private Sub Command1_Click()
Dim counter As Integer
counter = 1
Do While counter
7/28/2019 Controll Structures
83/89
executes the statements first
and then tests condition after each execution.This variation guarantees
at least one execution ofstatements:
Do
statementsLoop While condition
Another variation of the
Do...Loop statement
Do while
IT Department - National Institute of Education End Show
Write a program to Display Sri lanka 10 times
7/28/2019 Controll Structures
84/89
Write a program to Display Sri lanka 10 times
Private Sub Command1_Click()
Dim i As Integer
Do
Print "Sri Lanka"
i = i + 1
Loop While i < 10
End Sub
IT Department - National Institute of Education End Show
7/28/2019 Controll Structures
85/89
Private Sub Command1_Click()
Dim Sum As Integer
Sum = 0
Do
Sum = Sum + 5
Print Sum
Loop While Sum
7/28/2019 Controll Structures
86/89
Do Until conditionstatements
Loop
Do Until Loop
Do Until/Loop structure will
execute until condition isfalse
IT Department - National Institute of Education End Show
Do Until Loop Cont
7/28/2019 Controll Structures
87/89
Do Until Loop Cont
Private Sub Command1_Click()Dim Sum As Integer
Sum = 0
Do Until Sum >= 50
Sum = Sum + 5
Print Sum
Loop
End Sub
IT Department - National Institute of Education End Show
Do Until Loop Cont
7/28/2019 Controll Structures
88/89
Do Until Loop Cont
Private Sub Command1_Click()Dim Sum As Integer
Sum = 0
Do
Sum = Sum + 5
Print Sum
Loop Until Sum >= 50
End Sub
IT Department - National Institute of Education End Show
Control structures
7/28/2019 Controll Structures
89/89
Two other variations are analogous to theprevious two,
except that they loop as long
as conditionis False rather than True.
Loop zero or more times Loop at least once
Do Until condition
statementsLoop
Do
statementsLoop Until condition