Vous êtes sur la page 1sur 89

Control Structures

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.

IT Department - National Institute of Education

End Show

Control Structures Sequences Selection-(Branching)

Repetition-(Looping)

IT Department - National Institute of Education

End Show

Control Structures Selection (Branching) IF

Select Case
Goto

IT Department - National Institute of Education

End Show

Control Structures Repetition For Next Do While

Do until

IT Department - National Institute of Education

End Show

Control Structures -Selection


IF Statement Branching statements are used to cause certain actions within a program if a certain condition is met. Syntax If <condition > Then statement If (Balance Check) < 0 Then Print "You are overdrawn"

IT Department - National Institute of Education

End Show

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

Control Structures
If Then and End If blocks If Then and statements: Syntax End If blocks allows multiple

IF <condition> then statement 1 statement 2 ................ statement n end if


IT Department - National Institute of Education

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

IT Department - National Institute of Education

End Show

If Then end if
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
IT Department - National Institute of Education

End Show

If Then else end if


If Balance - Check < 0 Then Print "You are overdrawn" Print "Authorities have been notified" Else Balance = Balance - Check End If Here, the same two lines are printed if you are overdrawn (Balance - Check < 0), but, if you are not overdrawn (Else), your new Balance is computed.
IT Department - National Institute of Education

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

Marks <65 and >=55 Grade is C


Other wise Grade is D
End Show

IT Department - National Institute of Education

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

IT Department - National Institute of Education

End Show

Select Case

Visual Basic provides the Select Case structure as an alternative to If...Then...Else for selectively

executing one block of statements from among


multiple 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

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

Write a program to read marks and display Grade as Follows

Marks>=75 Grade is A
Marks<75 and >=65 Grade is B

Marks <65 and >=55 Grade is C


Other wise Grade is D
End Show
IT Department - National Institute of Education

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

The GoTo Statement Another branching statement.

The format is GoTo Label, where Label is a labeled line.

Labeled lines are formed by typing the Label followed by a colon.


.

IT Department - National Institute of Education

End Show

Line10: .. Goto Line10

IT Department - National Institute of Education

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

IT Department - National Institute of Education

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

IT Department - National Institute of Education

End Show

For...Next

When you know you must execute

statements a specific number of times,


ForNext loop is the better choice.

IT Department - National Institute of Education

End Show

Cont

For loop uses a variable called a

counter that increases or decreases


in value during each repetition of the loop.

IT Department - National Institute of Education

End Show

The syntax is:


For counter = start To end [Step statement Statement statement . increment]

Next [counter]
End Show

IT Department - National Institute of Education

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.

For i=1 to 100 step 5


IT Department - National Institute of Education

End Show

Cont
If increment is negative start must be greater than or equal to end for the body of the loop to execute.

For i=100 to 1 step -5


If Step isn't set, then increment defaults to 1. In executing the For loop, Visual Basic: Sets counter equal to start.

IT Department - National Institute of Education

End Show

In executing the For loop, Visual Basic:


1. Sets counter equal to start.

2. Tests to see if counter is greater than end. If this is so, Visual Basic exits the loop.

3. Executes the statements.


4. Increments counter by 1 or by increment, if it's specified. 5. Repeats steps 2 through 4.
IT Department - National Institute of Education

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]

FOR counter = start TO end [STEP

statement
Statement statement

Executes the statements.

NEXT [counter]

IT Department - National Institute of Education

End Show

Cont

4 increment]

FOR counter = start TO end [STEP

statement
Statement statement NEXT [counter]

IT Department - National Institute of Education

End Show

Cont

4 increment]

FOR counter = start TO end [STEP

statement
Statement statement NEXT [counter]

IT Department - National Institute of Education

End Show

Cont

4 increment]

FOR counter = start TO end [STEP

statement
Statement statement

NEXT [counter]

IT Department - National Institute of Education

End Show

Cont

4 increment]

FOR counter = start TO end [STEP

statement
Statement statement

NEXT [counter]

IT Department - National Institute of Education

End Show

Cont

4 increment]

FOR counter = start TO end [STEP

statement
Statement statement

NEXT [counter]

IT Department - National Institute of Education

End Show

Cont

Sets counter equal to 1 Tests to see if counter is greater than 5

Increments counter by 1 or by increment, if it's specified.

2 5

4
1

FOR counter = 1 TO PRINT COUNTER NEXT counter 3

IT Department - National Institute of Education

End Show

Write a program to Sri Lanka Five times


Private Sub Command1_Click() Dim i As Integer

Cont

For i = 1 To 10
Print "Sri Lanka" Next End Sub

IT Department - National Institute of Education

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.

IT Department - National Institute of Education

End Show

Write a program to Display the Following numbers

IT Department - National Institute of Education

End Show

Private Sub Command1_Click() Dim i,j as Integer

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

End Show

Private Sub Command1_Click() Dim i,j as Integer 0

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

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

End Show

Private Sub Command1_Click() Dim i,j as Integer

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

End Show

Private Sub Command1_Click() Dim i,j as Integer

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

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

j<4

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

j<4 Yes

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

J<4 ?

i
1 1

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

J<4 ? Yes i 1 1

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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 2

End Show

Private Sub Command1_Click() Dim i,j as Integer 1 2

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 2

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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 2

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

J<4

i
1 1 1 2

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

J<4 yes

i
1 1 1 2

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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 2 3

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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 2 3

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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 2 3

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

J<4 ?

i
1 1 1 1 2 3

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

J<4 ? Yes

i
1 1 1 1 2 3

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

Private Sub Command1_Click() Dim i,j as Integer 1

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

Private Sub Command1_Click() Dim i,j as Integer 1

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

J<4 ?

i
1 1 1 1 1 2 3 4

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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 J<4 No
1 1 1 1 1 2 3 4

End Show

Private Sub Command1_Click() Dim i,j as Integer 1

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

Private Sub Command1_Click() Dim i,j as Integer 1

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

Private Sub Command1_Click() Dim i,j as Integer 1

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

Private Sub Command1_Click() Dim i,j as Integer 2

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

Private Sub Command1_Click() Dim i,j as Integer

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

Private Sub Command1_Click() Dim i,j as Integer

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

Private Sub Command1_Click() Dim i,j as Integer 2

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

Private Sub Command1_Click() Dim i,j as Integer 2

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

Private Sub Command1_Click() Dim i,j as Integer 2

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

J<4 ?

i
1 1 1 1 1 2 3 4

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

J<4 ? Yes

i
1 1 1 1 1 2 3 4

End Show

Private Sub Command1_Click()

Dim i,j as Integer


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

J<4 ? Yes

i
1 1 1 1 2 1 2 3 4 1

End Show

Write a program to read 6 number and Display total


Private 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 Education

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.

IT Department - National Institute of Education

End Show

Do while

Do...Loop Use a Do loop to execute a block of statements an 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. Do...Loop, the statements execute as long as the condition is True:
IT Department - National Institute of Education

End Show

Do while Do While condition statements 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.
IT Department - National Institute of Education

End Show

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 Education

End Show

Do while If condition is False (zero), it skips past all the statements.


Private Sub Command1_Click() Dim counter As Integer counter = 1 Do While counter <= 15 Print counter counter = counter + 1 Loop

End Sub
IT Department - National Institute of Education

End Show

Do while

Another variation of the Do...Loop statement


Do statements Loop While condition

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

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

Private Sub Command1_Click() Dim Sum As Integer Sum = 0 Do Sum = Sum + 5 Print Sum

Loop While Sum <= 50


End Sub
End Show

IT Department - National Institute of Education

Do Until Loop Do Until condition statements Loop

Do Until/Loop structure will execute until condition is false


IT Department - National Institute of Education

End Show

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


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
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

IT Department - National Institute of Education

Vous aimerez peut-être aussi