Académique Documents
Professionnel Documents
Culture Documents
Control structures allow you to control the flow of your program's execution.
If left unchecked by control-flow statements, a program's logic will flow through statements from left to right, and top to bottom.
End Show
Repetition-(Looping)
End Show
Select Case
Goto
End Show
Do until
End Show
End Show
End Show
Control Structures
If Then and End If blocks If Then and statements: Syntax End If blocks allows multiple
End Show
If Then end if
Private Sub Command1_Click() number = Text1.Text If number Mod 2 = 0 Then MsgBox ("Even") End If End Sub
End Show
If Then end if
Private Sub Command1_Click()
check = 9678.78
If balance - check < 0 Then Print "You are overdrawn"
End Show
End Show
If Then else end if Write a program to read marks and display Grade as Follows
Marks>=75 Grade is A
Marks<75 and >=65 Grade is B
Private Sub Command1_Click() Dim Marks As Integer Dim Grade As String Marks = 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 If MsgBox (Grade) End Sub
IT Department - National Institute of Education
End Show
Select Case
Select Case testexpression [Case expressionlist1 [statementblock-1]] [Case expressionlist2 [statementblock-2]] . . . [Case Else [statementblock-n]] End Select
End Show
Select Case
Visual Basic provides the Select Case structure as an alternative to If...Then...Else for selectively
End Show
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 1 MsgBox ("Odd Number") End Select End Sub
IT Department - National Institute of Education
End Show
Select Case
Marks>=75 Grade is A
Marks<75 and >=65 Grade is B
Select Case
Private Sub Command1_Click() Dim Marks As Integer Marks = InputBox("Enter integer") Select Case Marks Case 75 To 100 MsgBox ("A") Case 65 To 74 MsgBox ("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
End Show
End Show
Private Sub Command1_Click() Dim number As Integer disp1: number = InputBox("Enter Number between 0 to 100") If number <= 100 Then MsgBox ("you entered " & number) Else GoTo disp1 End If End Sub
End Show
For...Next
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
End Show
For...Next
End Show
Cont
End Show
Next [counter]
End Show
Cont
The arguments counter, start, end, and increment are all numeric. The increment argument can be either positive or negative. If increment is positive, start must be less than or equal to end or the statements in the loop will not execute.
End Show
Cont
If increment is negative start must be greater than or equal to end for the body of the loop to execute.
End Show
2. Tests to see if counter is greater than end. If this is so, Visual Basic exits the loop.
End Show
Cont
Test to see if counter is greater than end. Set counter equal to start Increments counter by 1 or by increment, if it's specified.
4 increment]
statement
Statement statement
NEXT [counter]
End Show
Cont
4 increment]
statement
Statement statement NEXT [counter]
End Show
Cont
4 increment]
statement
Statement statement NEXT [counter]
End Show
Cont
4 increment]
statement
Statement statement
NEXT [counter]
End Show
Cont
4 increment]
statement
Statement statement
NEXT [counter]
End Show
Cont
4 increment]
statement
Statement statement
NEXT [counter]
End Show
Cont
2 5
4
1
End Show
Cont
For i = 1 To 10
Print "Sri Lanka" Next End Sub
End Show
1. Write a program to read ten numbers and display the total; 2. Write a program to read 15 numbers and display the average.
End Show
End Show
End Show
End Show
End Show
i<3 ?
End Show
i<3 ? yes
End Show
End Show
End Show
j<4
End Show
j<4 Yes
End Show
i
1 1
End Show
i
1 1
End Show
i
1 1
End Show
J<4 ?
i
1 1
End Show
J<4 ? Yes i 1 1
End Show
i
1 1 1 2
End Show
i
1 1 1 2
End Show
i
1 1 1 2
End Show
J<4
i
1 1 1 2
End Show
J<4 yes
i
1 1 1 2
End Show
i
1 1 1 1 2 3
End Show
i
1 1 1 1 2 3
End Show
i
1 1 1 1 2 3
End Show
J<4 ?
i
1 1 1 1 2 3
End Show
J<4 ? Yes
i
1 1 1 1 2 3
End Show
i
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
J<4 ?
i
1 1 1 1 1 2 3 4
End Show
i J<4 No
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
i<3 ?
For i = 1 To 3 For j = 1 To 4 Print i ,j Next j
Next i End Sub
IT Department - National Institute of Education
i
1 1 1 1 1 2 3 4
End Show
i<3 ? Yes
For i = 1 To 3 For j = 1 To 4 Print i ,j Next j
Next i End Sub
IT Department - National Institute of Education
i
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
i
1 1 1 1 1 2 3 4
End Show
J<4 ?
i
1 1 1 1 1 2 3 4
End Show
J<4 ? Yes
i
1 1 1 1 1 2 3 4
End Show
J<4 ? Yes
i
1 1 1 1 2 1 2 3 4 1
End Show
End Show
1. Write a program to read ten numbers and display the total. 2. Write a program to read 15 numbers and display the average.
End Show
Do while
End Show
End Show
Do While i < 10
Print "Sri Lanka" i=i+1 Loop End Sub
End Show
End Sub
IT Department - National Institute of Education
End Show
Do while
executes the statements first and then tests condition after each execution. This variation guarantees at least one execution of statements:
IT Department - National Institute of Education
End Show
End Show
Private Sub Command1_Click() Dim Sum As Integer Sum = 0 Do Sum = Sum + 5 Print Sum
End Show
Print Sum
Loop End Sub
IT Department - National Institute of Education
End Show
Print Sum
Loop Until Sum >= 50 End Sub
IT Department - National Institute of Education
End Show
Control structures
Two other variations are analogous to the previous two, except that they loop as long as condition is False rather than True.
Loop zero or more times Do Until condition statements Loop Loop at least once Do statements Loop Until condition