Lesson 4 Loop Statement
Lesson 4 Loop Statement
Prepared by:
MERCY ANN G. GIERE
LOOP STRUCTURE
Loop statements allow one or more
lines of code to be executed a
number of times.
1. Do …While
The Do…While is used to execute a
block of statements in an unspecified
number of times while a condition is
True. If condition is false on the first
pass, the statements are not
executed.
Syntax :
Do While
condition Statements
Loop
OR
Do
Statements
Loop While Condition
Example 1:
Dim Number as Integer
Number = 10
Do While Number < 20
Number= Number + 2
Print Number
Loop
Example 2:
Dim Number as Integer
Number = 10
Do
Number= Number + 2
Print Number
Loop While Number <20
2. Do …Until
The Do… Until is used to execute a
block of statements in an
unspecified number of times while
a condition becomes True. If
condition is True on the first pass,
the statements are not executed.
Syntax:
Do Until
condition statements
Loop
OR
Do
Statements
Loop Until condition
Example 1:
Dim Number as Integer
Number = 30
Do Until Number <= 20
Number=Number -2
Print Number
Loop
Example 2:
Dim Number as Integer
Number = 30
Do
Number = Number -2
Print Number
Loop Until Number <= 20
3. For …Next
The For…Next Loop is used when you
know the how many times the
statements of the loop will be
executed. It uses a variable that
serves as a counter that increases or
decreases in value every time there is
a loop.
In the For … Next Loop Execution:
1. The counter is set to the value of
start.
2. Counter is checked to see if it is
greater than the end. If yes, it
passes to the statements after the
next, if not, the statements are
executed.
3. The counter is incremented by the
amount specified. (If not specified,
the counter is incremented by 1.)
4. Counter is incremented and goes
back to step 2.
Syntax:
For counter = start to end
Statements
Next
OR
For counter = start To end (Step
Increment)
Statements
Next
Example 1:
Dim Num as Integer
For Num = 10 To 20
Print Num
Next Num
Example 2:
Dim Num as Integer
For Num = 10 To 100 Step 10
Print Num
Next Num
Example 3:
Dim Num as Integer
For Num = 3 To 30 Step 3
Print Num
Next Num