Unit 5 - Repetition (or Iteration) structure
The repetition (or iteration) structure permits a sequence of the instructions to be executed repeatedly until certain condition is reached. The repetition structure or loop in C++ comes in three forms while, do-while and for.
a) The while loop
The while loop repeats the body of the loop as long as the loop condition holds. The basic form of the while statement is as below:
while (expression) statement;
In this loop, the expression is first evaluated. If it is true (not zero), the statement (which can be a block is executed; if it is (zero), the statement is bypassed.
b) The do-while loop
The do-while loop executes a statement as long as the loop condition holds. The basic form of the do-while statement as below:
Do
statement
while (expression) ;
This do-while loop is quite similar to the while loop except that the expression is evaluated after the statement is executed. This means the statement in the do-while will be executed at least once. In the while statement, the statement will not be executed if the expression is false.
c) The for Loop
The for loop repeats the body of the loop as long as the loop condition holds. The basic form of the for loop as below:
for (initialization; condition test; incrementation)
{
statements;
}
• The initialization part typically refers to the initial value (if any) given to a loop variable or counter. But it can also include the initialization of any other variable. This initialization part is carried out just once at the beginning of the loop.
• The expression part determines whether the loop execution should be continued. If the expression is zero (false), the for loop is terminated, if it is not zero (true), the for statement is executed.
• The incrementation part typically increments (or decrements) the loop counter variable. This is done after the for statement is executed. This part, like the initialization part, can also include the incrementation of other variables.
The example 5.9, given is a very short program and it is easy for us to understand the for loop.
First, an integer variable is declared. Then, in the initialization part, the variable, iNum is set to 1. For the condition checking, iNum is checked to see whether it is equal to or less than 10. In each cycle of the loop, iNum is incremented by 1. Once iNum reaches 10, the loop exits. We can see that the program calculates the square of the first ten natural numbers.
Program output:
1 4 9 16 25 36 49 64 81 100
a) The while loop
The while loop repeats the body of the loop as long as the loop condition holds. The basic form of the while statement is as below:
while (expression) statement;
In this loop, the expression is first evaluated. If it is true (not zero), the statement (which can be a block is executed; if it is (zero), the statement is bypassed.
b) The do-while loop
The do-while loop executes a statement as long as the loop condition holds. The basic form of the do-while statement as below:
Do
statement
while (expression) ;
This do-while loop is quite similar to the while loop except that the expression is evaluated after the statement is executed. This means the statement in the do-while will be executed at least once. In the while statement, the statement will not be executed if the expression is false.
c) The for Loop
The for loop repeats the body of the loop as long as the loop condition holds. The basic form of the for loop as below:
for (initialization; condition test; incrementation)
{
statements;
}
• The initialization part typically refers to the initial value (if any) given to a loop variable or counter. But it can also include the initialization of any other variable. This initialization part is carried out just once at the beginning of the loop.
• The expression part determines whether the loop execution should be continued. If the expression is zero (false), the for loop is terminated, if it is not zero (true), the for statement is executed.
• The incrementation part typically increments (or decrements) the loop counter variable. This is done after the for statement is executed. This part, like the initialization part, can also include the incrementation of other variables.
The example 5.9, given is a very short program and it is easy for us to understand the for loop.
First, an integer variable is declared. Then, in the initialization part, the variable, iNum is set to 1. For the condition checking, iNum is checked to see whether it is equal to or less than 10. In each cycle of the loop, iNum is incremented by 1. Once iNum reaches 10, the loop exits. We can see that the program calculates the square of the first ten natural numbers.
Program output:
1 4 9 16 25 36 49 64 81 100
No comments:
Post a Comment