Repetition Control Flow Structures
Repetition Control Flow Structures
Facilitators
Mr. Luyima Alex Cedric
[email protected]
0772 302775 / 0708 045 305
statement(s);
*The construct includes the initialization of the counter, the condition and the increment.
The main function of the for loop is to repeat the statement(s) while the condition remains true. The
condition should check for a specific value of the counter. In addition, it provides ways to initialize the
counter and increment (or decrement) the counter. Therefore, the for loop is designed to perform a
repetitive action for a pre-defined number of times. Consider the following example:
Program-5.1 Explanation: In the above example, the variable “x” starts with an initial value of “80”.
The second expression inside the parenthesis determines the number of repetitions of the loop. It reads
as; “as long as “x” is less than or equal to 85 repeat the statement(s)”.
The third expression is the incrimination of x; it is achieved by the ++operator. You may also decrement
x depending on the requirement, but you have to use suitable control expression and an initial value. In
the first round of execution “x” is set to “80”. Then the expression “x<= 85” is evaluated. Since the
current value of the “x “is “80” expression is evaluated as TRUE.
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -2-
Therefore, the printf function gets executed. Then “x” is incremented by the ++operator and now its new
value becomes “81”. At this point the first round of the loop is completed. Then in the second round the
expression is evaluate d again. Since “x” is “81” the expression will be TRUE. Therefore, the printf
function gets executed for the second time.
Then “x” is incremented once more and its new value becomes “82”. This process continues for another
3 rounds. After a total of six rounds “x” becomes “86”. When the expression is evaluated at the beginning
of the sixth round “x” is greater than 85 therefore expression becomes FALSE. Then the loop will
terminate and the control is given to rest of the instructions which are outside the loop.
Example 5.2: Write down program that will output the following on the screen:
21 24 27 30 33 36 39 42
From the output given above, we identify that there is a common difference of 3 between the successive
integer outputs.
Example 5.3: Write down a program that will output the following on the screen:
96 90 84 78 72 66 60
From the output given above, we identify that there is a common difference of 6 and the output is
descending.
Example 5.4 – Write a program to calculate the sum of all the even numbers up to 100.
Solution
• First even number is 0 and the last even number is 100. By adding 2 to an even number the next
even number can be found.
• Therefore, the counter should be incremented by 2 in each round. Program-5.4 is an
implementation of the above requirement.
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -3-
3.4.2 The while Loop
The while loop construct contains only the condition. The programmer has to take care of the other
elements (initialization and incrementing / decrementing) inside the block. The general form of the while
loop is:
while (condition)
{
statement(s);
}
The loop is controlled by the logical expression that appears between the parentheses. The loop continues
as long as the expression is TRUE.It will stop when the condition becomes FALSE. You need to make
sure that the expression will stop at some point otherwise it will become an infinite loop.The while loop
is suitable in cases where the exact number of repetitions is not known in advance. Consider Example-
5.5:
OUTPUT:
80 81 82 83 84 85
From the output given above, we identify that there is a common difference of 6 but this time, the output
is descending. Therefore, the program would look like this:
Example 5.8: Write down a program that will output the following on the screen:
96 90 84 78 72 66 60
OUTPUT:
80 81 82 83 84 85
• In summary, the for loop is recommended for cases where the number of repetitions is known in
advance.
• The while loop is recommended for cases where the number of repetitions are unknown or
unclear during the development process.
• The do- while loop is recommended for cases where the loop to be executed needs to run at least
once regardless of the condition.
• However, each type of loop can be interchanged with the other two types by including proper
control mechanisms.
During the first 3 iterations the program executes normally displaying the numbers 1 and 2. Then the
moment the variable “n” becomes “3”, 3 is displayed and then the if condition which evaluates
whether “n==3” becomes TRUE, so it will execute the printf function and then the break instruction.
At this point the loop will terminate because of the break keyword.
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -7-
3.4.6 The continue Keyword
The keyword continue causes the program to skip the rest of the loop in the current iteration, causing it to jump
to the next iteration. Consider Example 5.12:
END
THANK YOU