FOR LOOP.
It allowed specifying
three things about a loop is a single line:
Ø Setting
a loop counter to an initial value.
Ø Testing
the loop counter to determine whether its value has reached the number of
repetitions desired.
Ø Increasing the value of loop counter each time the program segment has been executed.
General form of FOR LOOP statement:
It can be seen that three stapes are required for
the loop construct have now been incorporated in the FOR LOOP statement:
Ø Initialization
Ø Testing
Ø Incrementation
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigb62bd19vRVGNNBzoOuG5nvWtw5CSNyZwoR_6_VTVBxNbJsq1ebsClQC860ia7gzF2vXSYQN7UVU4XpzDAtYMU1UlndL_NjHQuFOtzXR_J5JTfCvJZTYJRcRzdYzpwcIFii6wlYN2AHDJ/w400-h320/for.png)
Execution of FOR LOOP
statement:
Ø When
for statement is executed for the first time, the value of count set to an
initial value 1.
Ø Now
the condition “count<=3” tested. Since count is 1, the condition is
satisfied and body of the loop is executed for the first time.
Ø Upon
reaching the closing brace of for, a computer sends the control back to the for the statement, where the value of count gets incremented by 1.
Ø Again
the test is performed to check whether the new value of count exceeds 3.
Ø The
body of the FOR LOOP statement continuous to get executed till count doesn’t
exceed the final value 3.
Ø When count reach the value 4, the control
exits from the loop and is transfer to the statement immediately after the body
for FOR LOOP.
Comments
Post a Comment