11-14 Repetitive Structures For, While Loops Do While
11-14 Repetitive Structures For, While Loops Do While
05/16/2025 1
• Loop contains the following parts:
• Control Structure: A variable which start with an initial value and determines the
duration of the repetition is known as control variable
• Body of the loop: A set of statements, which are executed within the loop
simultaneously.
• Test Condition: Each loop contains a test condition. The loop has to be repeated
or terminated, depends on test conditions. If the test condition is true, otherwise
terminates.
05/16/2025 2
Loop Control Structures
• Loops
Unfixed Iterations: While and Do-While loop
Fixed Iterations: For loop
• Unfixed Iterative Loops:
• This types of looping construct is used when the number of iterations is
unknown. In this situation, the loop repeats the execution of the belonging
statements till the given condition is true.
• Fixed Iterative Loops:
• This types of looping structure, the statements are repeated for fixed number of
times. The control terminates after repeating number of times. The control
terminates after repeating the execution of the statements for a given number of
times.
• Syntax :
• Initial Value Test Condition Update
• For(a=1; a<=10; a++)
05/16/2025 4
Controlling the program flow
• Forms of controlling the program
flow:
– Executing a sequence of statements
– Using a test to decide between
alternative sequences (branching) Statement1
Statement2
– Repeating a sequence of statements Statement3
(until some condition is met) Statement4
(looping) Statement5
Statement6
Statement7
Statement8
05/16/2025 5
Program Looping
• A set of statements that executes repetitively for a number of
times.
• Simple example: displaying a message 100 times:
printf(hello !\n”);
printf(hello !\n”)
printf(hello !\n”)
…
Repeat 100 times
printf(hello !\n”)
printf(hello !\n”)
printf(hello !\n”)
05/16/2025 6
05/16/2025 7
The need for program looping
05/16/2025 9
Iterative (loop) control structures
Each loop control structure will have
Program loop: body of loop.
control statement tests certain conditions & then directs repeated
execution of statements within the body of loop.
1) Entry controlled loop: control is tested before the start of the loop. If
false, body will not be executed.
2) Exit controlled loop: test is performed at the end of the body. i.e. body
of loop executed at least once.
05/16/2025 10
Entry Controlled & Exit controlled loops
Entry
Entry
Test False
Body of
Condition
The loop
True
Body of
True
The loop Test
Condition
False
05/16/2025 11
05/16/2025 12
05/16/2025 13
05/16/2025 14
#include<stdio.h>
#include<conio.h>
int main()
{
int i=3;
while(i!=0)
{
printf("Meow");
i=i-1;
}
getch();
return 0;
05/16/2025 15
05/16/2025 16
05/16/2025 17
05/16/2025 18
05/16/2025 19
05/16/2025 20
05/16/2025 21
05/16/2025 22
05/16/2025 23
05/16/2025 24
05/16/2025 25
05/16/2025 26
05/16/2025 27
• The order of execution of the loop is as follows:
• After executing the loop body, kt return back to the for statement and
increment/decrement value of a by 1;
• The steps are repeated from (2) and finally the control exits the loop, as
soon as the test condition is fasle.
05/16/2025 28
• For example
• Int I, s=0;
• For(i=1;i<=5;i++)
• S =S+I;
• Printf(“Sum=%d”,s);
Sum=1
Sum=3
Sum=6
Sum=10
Sum=15
05/16/2025 29
05/16/2025 30
05/16/2025 31
05/16/2025 32
05/16/2025 33
05/16/2025 34
05/16/2025 35
05/16/2025 36
05/16/2025 37
For Loop
05/16/2025 38
05/16/2025 39
05/16/2025 40
05/16/2025 41
05/16/2025 42
The do – while statement
General form:
do
{
body of the loop
}
while(test condition);
Exit controlled loop. At the end of the loop, the test condition is evaluated.
After do statement, program executes the body of the Loop.
Then, the condition is tested, if it is true, body of the loop is executed once again
& this process continues as long as the condition is true.
Body of the loop is executed at least once.
do-while loop can be nested.
05/16/2025 43
05/16/2025 44
05/16/2025 45
The do statement
do
program
statement
while Loop with the
( loop_expression ); test at the end !
Body is
executed at least
once !
3 1 Statement(s)
yes
loop_expression
2
No
Next statement 4
05/16/2025 46
05/16/2025 47
05/16/2025 48
Example – 200th triangular number
Statement before triangularNumber = 0
loop
init_expression n=1
no
loop_condition n<=200
yes
triangularNumber =
Statement(s) triangularNumber + n
loop_expression n=n+1
05/16/2025 49
The ‘for’ loop
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}
1 init_expression
no
5 2 loop_condition
yes
3 Program statement
4 Loop expression
Next Statement
05/16/2025 50
How for works
• The execution of a for statement proceeds as follows:
1. The initial expression is evaluated first. This expression usually sets a
variable that will be used inside the loop, generally referred to as an
index variable, to some initial value.
2. The looping condition is evaluated. If the condition is not satisfied (the
expression is false – has value 0), the loop is immediately terminated.
Execution continues with the program statement that immediately
follows the loop.
3. The program statement that constitutes the body of the loop is executed.
4. The looping expression is evaluated. This expression is generally used to
change the value of the index variable
5. Return to step 2.
05/16/2025 51
The for statement
sum = 0;
no
1 2 5 4
for ( n = 1; n yes
<= 200; n = n + 1 )
{ sum = sum + n; }
Next Statement
for ( init_expression; loop_condition; loop_expression )
{ program statement(s)
}
05/16/2025 52
Finding sum of natural numbers up to 100
#include <stdio.h>
int main()
{
int n;
int sum;
sum=0; //initialize sum
05/16/2025 53
Infinite loops
• It’s the task of the programmer to design correctly the algorithms so
that loops end at some moment !
// Program to count 1+2+3+4+5
#include <stdio.h>
int main()
{
What is wrong here ?
int i, n = 5, sum =0; Does the loop end?
for ( i = 1; i <= n; n = n + 1 )
{
sum = sum + i;
printf(“%d”,sum);
}
return 0;
}
05/16/2025 54
Example – for with a body of 2 statements
#include <stdio.h>
int main()
{
int n, triangularNumber=0;
05/16/2025 55
for loop variants
• Multiple expressions (comma between…)
for(i=0 , j=10 ; i<j ; i++ , j--)
• Declaring variables
for(int i=0 ; i=10 ; i++ )
05/16/2025 56
while-loop
General format:
05/16/2025 57
The while statement
while ( expression )
program statement Loop with the
test in the
beginning !
statement before loop Body might
never be
executed !
3 1 Loop_expression 4
No
yes
2 statement (s)
Next statement
05/16/2025 58
Finding sum of natural numbers up to 100
05/16/2025 59
Program to reverse the digits of a number
#include <stdio.h>
int main()
{
int number, rev=0, right_digit;
while ( number != 0 )
{
right_digit = number % 10;
rev=rev*10 + right_digit;
number = number / 10;
}
printf(“The reversed number is %d“, rev);
return 0;
}
05/16/2025 60
Example: Finding sum of natural numbers
up to 100
#include <stdio.h>
#include <stdio.h> int main()
int main()
{
{
int number, rev=0, right_digit;
int number, rev=0, right_digit;
printf(“Enter your number.\n“);
printf(“Enter your number.\n“);
scanf(“%d”,&number);
scanf(“%d”,&number);
do
while ( number != 0 )
{
{
right_digit = number % 10;
right_digit = number % 10;
rev=rev*10 + right_digit;
rev=rev*10 + right_digit;
number = number / 10;
number = number / 10;
}
}
while ( number != 0 );
printf(“The reversed number is %d“, rev);
printf(“The reversed number is %d“,rev);
return 0;
return 0;
}
}
05/16/2025 62
Which loop to choose ?
• Criteria: category of looping
• Entry-controlled loop -> for, while
• Exit-controlledloop -> do
• You can actually rewrite any while as a for and vice versa !
05/16/2025 63
05/16/2025 64
Nested For Loop
• Nested loops are programming structures where one or
more loops are placed inside another loop.
2.Inner Loop Execution: When the control reaches the inner loop, it
follows a similar process: initialization, condition check, and
execution of statements.
3.Nested Iterations: For each iteration of the outer loop, the inner
loop undergoes its complete set of iterations.
int main()
{
int rows = 5;