SESSION SEVEN
ITERATIVE STATEMENTS
7.1 Introduction
7.2 Learning Outcomes
7.3 Iterative Statements
7.4 While Loop
7.5 do-while loop
7.6 Difference between while-loop and do-while loop
7.7 for loop
7.8 Summary
7.1 Introduction
In our previous session, we learnt about sequential and decision control
structure. Decision control statements allow us to select which statements to
execute and which statements to skip. However, sometimes we require
executing same set of statements finite number of times. In such situation,
we can use loops. In this session, we will discuss about the iterative control
statements.
7.2 Learning Outcomes
At the end of this session, you should be able to:
Demonstrate knowledge and understanding of iterative
control statements in C language.
To define the basic syntax of iterative control
statements.
Create programs using While, Do while and For
statements.
7.3 Iterative Statements
Iterative structure is used to execute a certain set of actions for a predefined
number of times or until a particular condition is satisfied. It’s also called
repetitive or loop structure because it repeats or iterates a set of statements
until a certain predefined condition is met.
There are 3 iterative control statements available in C to implement iterative
structures namely:
While statement
Do while statement and
For statement
Figure 7.1: Loop Structure Flowchart
7.4 While Loop
While loop is an entry-controlled loop. The statements inside braces are
allowed to execute only if condition inside while is evaluates to TRUE. While
statement is suited for the problems where it is not known in advance that
how many times a statement or block of statements will be executed
Syntax-
Initialization of loop variable
while (condition using loop variable)
{
Statements (s);
Increment/decrement of loop variable;
}
A condition using loop variable is specified with while-statement. If the
condition is FALSE then the statements inside while block will be skipped, If
the condition is TRUE then the statements inside while block will be executed
again and again until the condition becomes FALSE.
Example- Write a Program in C to print your name 10 times
Flowchart-
Flowchart- Example
Programs using while loop
Example-1 –
Write a program in C to print the numbers from 1, 2, 3..... 10
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i;
// Processing and Output
i = 1;
while ( i < = 10 )
{
printf ( “\n %d “, i);
i = i + 1;
}
}
Example-2 –
Write a program in C to print the numbers from 10, 9, 8..... 1
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i;
// Processing and Output
i = 10;
while ( i > = 1 )
{
printf ( “\n %d “, i);
i = i - 1;
}
}
Example-3 –
Write a program in C to print the numbers from N, N-1, N-2.....3, 2, 1
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = n;
while ( i > = 1 )
{
printf ( “\n %d “, i);
i = i - 1;
}
}
Example-4
Write a program in C to print the numbers from 1, 2, 3, ... N-2, N-1, N
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 1;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i + 1;
}
}
Example-5 –
Write a program in C to print the numbers from 2, 4, 6 ... N-2, N
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 2;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i + 2;
}
}
Example-6 –
Write a program in C to print the numbers from 200, 400, 600 ... N-
200, N
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 200;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i + 200;
}
}
Example-7 –
WAP in C to print the numbers from 2, 4, 8, 16 ... N/2, N
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 2;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i * 2;
}
}
Example-8 –
Write a program in C to print the numbers from N, N/2, ... 16, 8, 4, 2, 1
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = n;
while ( i > = 1 )
{
printf ( “\n %d “, i);
i = i / 2;
}
}
Example-9 –
A number is input through the keyboard. Write a program in C to print
the table of that number.
#include < stdio.h >
void main ( )
{
// Variable Declaration
int i, n, ans ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 1;
while ( i < = 10 )
{
ans = n * i;
printf ( “\n %d “, ans);
i = i + 1;
}
}
Example-10 –
A number is input through the keyboard. Write a program in C to print
the sum of its digits. [Hint: n= 1257 ans = 1+2=5+7 = 15]
#include < stdio.h >
void main ( )
{
// Variable Declaration
int n, ans, a ;
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &); //1257
// Processing and Output
ans =0;
while ( n ! = 0 )
{
a = n % 10; //1
ans = ans + a; // 15
n = n / 10; //0
}
printf ( “\n %d “, ans); //15
7.5 do-while loop
Syntax-
Initialization of loop variable
do
{
Statements (s);
Increment/decrement of loop variable;
} while ( condition using loop variable );
A condition using loop variable is specified with while-statement. If the
condition is FALSE then the control will come out of the while block, if the
condition is TRUE then the statements inside while block will be executed
again and again until the condition becomes FALSE.
7.6 Difference between while-loop and do-while loop
Example-
Flowchart-
7.7 for loop
A condition using loop variable is specified with for-statement. If the
condition is FALSE then the statements inside while block will be skipped, If
the condition is TRUE then the statements inside while block will be executed
again and again until the condition becomes FALSE.
The difference between while loop and for loop is just the difference of their
syntax, otherwise, logically they works in the same way.
In for loop, initialization of loop variable, condition using loop variable and
Increment/decrement of loop variable are specified in the same line whereas
in while-loop, they are specified in different lines as shown above.
Example- WAP in C to print your name 10 times
Flowchart-
Same as that of while-loop
7.8 Summary
We have discussed the three programing constructs: sequential, selection and
iterative. We hope you have enriched your programming experience through
various examples used. In our next lecture, we shall discuss about arrays.