Professional Documents
Culture Documents
12
(MS Access and C)
while is a pretest
loop (the condition
is evaluated before
the loop executes)
Condition is evaluated:
if it is true, the statement(s) are executed, and then condition is evaluated again
if it is false, the loop is exited
Produces output:
Counter Variable
A variable that is incremented or decremented each time a loop iterates
It can be used to control the execution of the loop (as a loop control variable)
It must be initialized before entering loop
It may be incremented/decremented either inside the loop or in the loop test
Execution continues as long as the condition is true; the loop is exited when the condition becomes false
; after (condition) is also required
The loop executes at least once even if the condition is false in the beginning
The inner loop goes through all its iterations for each iteration of the outer loop
break can
The inner loop goes through all its iterations for each iteration of be
theused to loop
outer
terminate the
execution of a loop
iteration
The counter variable x indicates that the loop should execute for five times
But it is executed only once
In first iteration, the printf statement in the loop displays “Questioning” and control moves to break
statement
This statement moves the control out of the loop. So the message appears only once
The inner loop goes through all its iterations for each iteration of the outer loop
The inner loop goes through all its iterations for each iteration of the outer loop
Total number of iterations for inner loop is product of number of iterations of the two loops
In example, Outer loop executes two times and inner loop executes three times with each iteration of outer
loop
The inner loop iterates 6 times in total