We have also discussed about iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met. It can be predefined as in the loop, or open ended as in while and do-while. There are three types of Iterative statements: (1) The For statements ,(2)The While statements (3)The Do-While statements.
We must also be aware of the following when using for statements. The For statement or for loop is considered as a predefined loop because the number or times it iterates to perform its body is predetermined in the loop’s definition. The For loop contains a counter whose values determine the number of times the loop iterates. The iteration stops upon reaching the number of times specified in the loop. Never place a semicolon right after the for header. Never change the value of the for loop’s counter in side the body of the loop. This will affect the result of the program. The increment part of the for loop is execute after the first iteration of the loop.
The while statement or while loop is an open-ended or event-controlled loop. The second type of open-ended or event-controlled loop is the do-while statement or do-while loopDo-While is a variation of the while statement which checks the condition at the bottom / end of the loop.This means that a do-while loop “always executes at least once”. In the do-while loop, when the condition evaluates to TRUE (1), the loop body will be executed, but when FALSE (0), program control proceeds to the next instruction after the do-while loop.
General forms:
FOR STATEMENT
for (initialization; condition; increment)
{
statement_sequence;
}
WHILE STATEMENT
while (condition)
{
statement_sequence;
}
DO-WHILE STATEMENT
do
{
statement_sequence;
} while (condition);
No comments:
Post a Comment