0% found this document useful (0 votes)
2 views

Repetition Control Flow Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Repetition Control Flow Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1107 Principles of Programming

Repetition Control Flow Structures in C

Facilitators
Mr. Luyima Alex Cedric
[email protected]
0772 302775 / 0708 045 305

Mr. Mugejjera Emmanuel


[email protected]
Phone: 0703 186705 / 0772 959183

Mr. Mawebe John Bosco


[email protected]
Phone: 0773-361111 / 0702 957911

- Ndejje University - IT 1107 / CS 1107 Principles of Programming -1-


3.4 Repetition Control Structures
A Program is usually not limited to a linear sequence of instructions or conditional structures and it is
sometimes required to execute a statement or a block of statements repeatedly. These repetitive
constructs are called loops or repetition control structures.
The C language supports three constructs; namely for, while and do- while loops. The rest of this section
introduces these repetition control structures.

3.4.1 The for Loop


The for loop construct is used to repeat a statement or block of statements a specified number of times. The
general form of a for loop is:

for (counter - initialization; condition; increment/decrement)

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:

Execution of Program-5.1 displays:


80 81 82 83 84 85

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

- Ndejje University - IT 1107 / CS 1107 Principles of Programming -4-


Example 5.6: 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 but this time, the output
is descending. Therefore, the program would look like this:

3.4.3 The do-while Loop


Another useful loop is the do- while loop. The only difference between the do-while loop and other loops
is that in the do-while loop the condition comes after the statement(s). It takes the following form:
do
{
statement(s);
}
while (condition);

Example 5.8: Write down a program that will output the following on the screen:
96 90 84 78 72 66 60

- Ndejje University - IT 1107 / CS 1107 Principles of Programming -5-


This means that the statement(s) inside the loop will be executed at least once regardless of the condition
being evaluated. Consider Example-5.7:

OUTPUT:
80 81 82 83 84 85

Conclusion on Repetition Control Flow Structures

• 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.

3.4.4 Nesting of Loops


Like the conditional structures loops can also be nested inside one another. You can nest loops of any
kind inside another to any depth you want. However, having large number of nesting will reduce the
readability of your source code.
Example-5.9 –Write a C program to display the following pattern using nesting loops:
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$

- Ndejje University - IT 1107 / CS 1107 Principles of Programming -6-


There are 10 “$” symbols in a single row (10 columns) and there are 5 rows. This can be implemented
by having a two loops one nested inside another which print individual “$” symbols.
However the printf function does not support displaying text on a row that you have already printed.
Therefore first you need to fully complete the first row and then you should go to the next. Therefore
the loop which handles printing of individual rows should be the outer loop and one which prints
elements within a row (columns) should be the inner loop.

3.4.5 The break Keyword


The break keyword is used to terminate a loop, immediately bypassing any conditions. The control will
be transferred to the first statement following the loop block. If you have nested loops, then the break
statement inside one loop transfers the control to the immediate outer loop. The break statement can be
used to terminate an infinite loop or to force a loop to end before its normal termination.

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

- Ndejje University - IT 1107 / CS 1107 Principles of Programming -8-

You might also like