Lesson 02
Iteration/Looping
in
C Programming
C Programming
Pijushkanti Panigrahi
What is Looping ?
The looping can be defined as repeating the same
process multiple times until a specific condition
satisfies. It is known as iteration also. There are
three
types
of
loops
used
in
the
C
.
In
three
types
of
loops
used
in
the
C
.
In
this part of the tutorial, we are going to learn all
the aspects of C loops..
2
Why looping?
The looping simplifies the complex problems into the
easy ones.
It enables to alter the flow of the program so that
instead
of
writing
the
same
code
again
and
again,
we
instead
of
writing
the
same
code
again
and
again,
we
can execute the same code for a finite number of
times.
For example, if we need to print UNIVERSITY OF
CALCUTTA 10-times then, instead of using the printf
statement 10 times, we can use printf once inside a
loop which runs up to 10 iterations.
3
What are the advantages of Looping?
1) It provides code reusability.
2) Using loops, we do not need to write the same
code again and again.
3) Using loops, we can traverse over the elements of
data
structures
(array
or
linked
lists)
.
data
structures
(array
or
linked
lists)
.
4
Types of C Loops
There are three types of loops in C
language those are given below:
while
do while
do while
for
5
Essential components of a loop
Counter
Initialisation of the counter with initial value
Condition to check with the optimum value
of the counter
of the counter
Statement(s) to be executed by iteration
Increment/decrement
6
Flowchart for a loop
7
while loop in C
The while loop in c is to be used in the scenario where
the block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is
also called a pre-tested loop.
The syntax of while loop in c language is given below:
The syntax of while loop in c language is given below:
initialisation;
while(condition)
{
block of statements to be executed ;
increment;
}
8
Write a C-program to print 10 natural numbers
#include<stdio.h>
main()
{
int i=1;
while(
i
<=10)
Output
1
2
3
4
while(
i
<=10)
{
printf("%d \n",i);
i=i+1;
}
}
9
5
6
7
8
9
10
Program to print table for the given number using while loop in C
#include<stdio.h>
main()
{
int i=1,number,b;
printf("Enter a number: ");
scanf("%d",&number);
Output
Enter a number: 5
5
10
15
20
25
while(i<=10)
{
b=number*i;
printf("%d \n", b);
i=i+1;
}
}
10
25
30
35
40
45
50
do-while loop in C
The do-while loop continues until a given condition
satisfies. It is also called post tested loop. It is used
when it is necessary to execute the loop at least once
(mostly menu driven programs).
The syntax of
do
-
while
loop in c language
is given below:
The syntax of
do
-
while
loop in c language
is given below:
do
{
code to be executed ;
}
while(condition);
11
Flowchart for do-while loop
12
#include<stdio.h>
main()
{
int i=1;
do
6.
i++;
7.}while(i<=10);
8.return 0;
9.}
Output
1 2 3 4 5 6 7 8 9 10
Output
1
2
3
do
{
printf("%d \n",i);
i=i+1;
}
while(i<=10);
}
13
3
4
5
6
7
8
9
10
Example: Program to add two integers
1 /* - - -- - - -*/
2 /* Program to prnt multipliction table of a number*/
3 #include <stdio.h>
4
5 main()
6 {
7 int number, i,m; /* declaration */
8 printf( "Enter the number\n" ); /* prompt */
9 scanf( "%d", &number); /* read a number */
10
i
=1;
/* read
a number
*/
Outline
1. Variables declaration
2. Input
14
10
i
=1;
/* read
a number
*/
11 do{ /* Starting of loop */
12 m=number*i; /* calculation of m*/
13 printf(“%d, ”, m); /*printing m */
14 }while(i<=10); /* condition testing*/
15
16
17 }
Enter first integer
5
5, 10, 15, 20, 25,30,35,40, 45,50,
2.1 loop starts
2.2 print values
3. Condition testing
4. end
Program Output
The for loop in C language is used to iterate the
statements or a part of the program several times. It
is frequently used to traverse the data structures like
the array and linked list.
The
syntax of for loop in c language is given below:
for loop in C
15
The
syntax of for loop in c language is given below:
for(Expression1; Expression2; Expression3)
{
codes to be executed;
}
Expression 1 (Optional)
Represents the initialization of the loop variable.
More than one variable can be initialised.
Expression 2
Expression 2 is a conditional expression. It checks for a
specific condition to be satisfied. If it is not, the loop is
terminated.
16
terminated.
Expression 2 can have more than one condition. However,
the loop will iterate until the last condition becomes false.
Other conditions will be treated as statements.
Expression 3
Expression 3 is increment or decrement to update the valu of
the loop variable
Flowchart : for loop
17
#include <stdio.h>
main()
{
int i;
for(
i
=1;i<=15;i=i+1)
Program to print natural numbers 1to15
18
for(
i
=1;i<=15;i=i+1)
{
printf("%d, ", i);
}
}
output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
Example: Program to add two integers
1 /* - - -- - - -*/
2 /* Addition program */
3 #include <stdio.h>
4
5 main()
6 {
7 int integer1, integer2, sum; /* declaration */
8
9 printf( "Enter first integer\n" ); /* prompt */
10
scanf( "%d", &integer1 );
/* read an integer */
Outline
1. Variables
declaration
2.
Input
19
10
scanf( "%d", &integer1 );
/* read an integer */
11 printf( "Enter second integer\n" ); /* prompt */
12 scanf( "%d", &integer2 ); /* read an integer */
13 sum = integer1 + integer2; /* assignment of sum */
14 printf( "Sum is %d\n", sum ); /* print sum */
15
16
17 }
Enter first integer
23
Enter second integer
24
Sum is 47
2.
Input
2.1 Sum
3. Print
Program Output
Practice such simple programs
Write a C program to calculate the perimeter and area of a land
Write a C program to swap the value of to variables with the help of
a third variable and without the help of a third variable.
Write a C program to find the large number among two
Write a C program to find the large number among three
Write a C program to print even and odd numbers from 1-50 using
while, for and do-while
Write a C program to find the largest number among ten
Write a C program to print the series 1 1 2 3 5 8 13 21
upto
150
Write a C program to print the series 1 1 2 3 5 8 13 21
upto
150
Write a C program to reverse a 3-digit integer
Write a C program to find the prime number
Write a C program to compile the mark-sheet of BLIS course
Write a C program to prepare a bill for 10 books after 20% discount
for your library
20