Lesson 02: Iteration/Looping in C Programming C Programming
Lesson 02: Iteration/Looping in C Programming C Programming
Iteration/Looping
in
C Programming
Pijushkanti Panigrahi
What is Looping ?
2
Why looping?
4
Types of C Loops
5
Essential components of a loop
• Counter
• Initialisation of the counter with initial value
• Condition to check with the optimum value
of the counter
• Statement(s) to be executed by iteration
• Increment/decrement
6
Flowchart for a loop
•
7
while loop in C
#include<stdio.h> Output
main() 1
2
{
3
int i=1; 4
while(i<=10) 5
6
{
7
printf("%d \n",i); 8
i=i+1; 9
10
}
}
9
Program to print table for the given number using while loop in C
#include<stdio.h> Output
main() Enter a number: 5
{ 5
int i=1,number,b; 10
15
printf("Enter a number: ");
20
scanf("%d",&number); 25
while(i<=10) 30
{ 35
b=number*i; 40
printf("%d \n", b); 45
50
i=i+1;
}
}
10
do-while loop in C
12
6.i++;
7.}while(i<=10);
8.return 0;
9.}
Output
1 2 3 4 5 6 7 8 9 10
#include<stdio.h> Output
main() 1
{ 2
int i=1; 3
do 4
{ 5
printf("%d \n",i); 6
i=i+1; 7
} 8
while(i<=10); 9
} 10 13
Example: Program to add two integers
1 /* - - -- - - -*/
2 /* Program to prnt multipliction table of a number*/
3 #include <stdio.h>
4 Outline
5 main()
6 {
1. Variables declaration
7 int number, i,m; /* declaration */
2. Input
8 printf( "Enter the number\n" ); /* prompt */
9 scanf( "%d", &number); /* read a number */
10 i=1; /* read a number */
11 do{ /* Starting of loop */ 2.1 loop starts
12 m=number*i; /* calculation of m*/
2.2 print values
13 printf(“%d, ”, m); /*printing m */
14 }while(i<=10); /* condition testing*/
3. Condition testing
15
4. end
16
17 }
14
for loop in C
15
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.
• 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
16
Flowchart : for loop
17
Program to print natural numbers 1to15
#include <stdio.h>
main()
{
int i;
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,
18
Example: Program to add two integers
1 /* - - -- - - -*/
2 /* Addition program */
3 #include <stdio.h>
4 Outline
5 main()
6 {
7 int integer1, integer2, sum; /* declaration */ 1. Variables
8 declaration
9 printf( "Enter first integer\n" ); /* prompt */ 2. Input
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 */ 2.1 Sum
14 printf( "Sum is %d\n", sum ); /* print sum */
3. Print
15
16
17 }
19
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 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