A loop contains set of statements which are executed again and again in a program till a condition is true. For Constructing a loop three steps must be performed. The first step is initialization of Conditions for repetition of loop, second step is increment, decrement or change of value for the identifier used for condition check and the third step is to check the condition for loop.
To explain more accurately we use same program example in while, do while and for loop:
1. While Loop:
While loop is used when a set of statement are to be executed again and again. The set of statements written in while will be executed until condition is true. If condition is false at the short, then while loop will not be executed.
FlowChart:

Syntax:
Initialize
while(expression)
{
………………
//set of statements
…………….
Increment/decrement
}
Example:
#include<stdio.h>#include<conio.h>void main(){int num, x=0, y=0,r=0;printf(“nenter a number”);scanf(“%d”,&num); //123while(num>0) // 123>0=>T, 12>0=>T , 1>0=>T, 0>0=>F{x++; // 1, 2 , 3y=y+num%10;r=r*10+num%10;num=num/10; // 123/10=>12, 12/10=>1 , 1/10=>0}printf(“nDigits=%d”,x);printf(“nSum of Digits =%d”,y);printf(“nReverse of a number is %d”,r);getch();}
Explanation of Program:
Header Files:
- The program includes two header files:
stdio.hfor standard input and output functions andconio.hfor console input and output functions. Note that the use ofconio.his non-standard and might not be supported by all compilers.Main Function:
- The
mainfunction is the entry point of the program.Variables:
num: Holds the input number.x: Counts the number of digits in the input.y: Stores the sum of the digits.r: Stores the reverse of the number.Input:
- The user is prompted to enter a number using
printfand the input is read usingscanf.While Loop:
- The program enters a
whileloop that continues as long asnumis greater than 0.- In each iteration:
xis incremented, counting the number of digits.yis updated by adding the last digit ofnum(obtained using% 10).ris updated to create the reverse of the number.numis updated by removing the last digit (using/ 10).Output:
- The program prints the number of digits, the sum of the digits, and the reverse of the number using
printf.getch():
- This function waits for a key press before the program exits. It’s part of the
conio.hlibrary.Overall, this program calculates and displays the number of digits, the sum of digits, and the reverse of a given number entered by the user.
2. do While Loop:
do While loop is somewhat similar to while loop, but in do while condition is checked after execution of loop. It is used when number of times set of statements in loop to be executed are not known in advance. It is used when loop is to be executed at least once.
FlowChart:

Syntax:
Initialize
do{
………………
//set of statements
…………….
Increment/decrement
}while(condition)
Example:
#include<stdio.h>#include<conio.h>void main(){int num, x = 0, y = 0, r = 0;printf(“nEnter a number: “);scanf(“%d”, &num); // 123do {x++; // 1, 2, 3y = y + num % 10;r = r * 10 + num % 10;num = num / 10; // 123/10=>12, 12/10=>1 , 1/10=>0} while (num > 0); // 123>0=>T, 12>0=>T , 1>0=>T, 0>0=>Fprintf(“nDigits = %d”, x);printf(“nSum of Digits = %d”, y);printf(“nReverse of the number is %d”, r);getch();}
Explanation of Program:
Header Files:
- The program includes two header files:
stdio.hfor standard input and output functions andconio.hfor console input and output functions. Note that the use ofconio.his non-standard and might not be supported by all compilers.Main Function:
- The
mainfunction is the entry point of the program.Variables:
num: Holds the input number.x: Counts the number of digits in the input.y: Stores the sum of the digits.r: Stores the reverse of the number.Input:
- The user is prompted to enter a number using
printfand the input is read usingscanf.do-while Loop:
- The program enters a
do-whileloop that always executes the loop body at least once before checking the loop condition.- In each iteration:
xis incremented, counting the number of digits.yis updated by adding the last digit ofnum(obtained using% 10).ris updated to create the reverse of the number.numis updated by removing the last digit (using/ 10).- The loop continues as long as
numis greater than 0.Output:
- The program prints the number of digits, the sum of digits, and the reverse of the number using
printf.getch():
- This function waits for a key press before the program exits. It’s part of the
conio.hlibrary.Overall, this program calculates and displays the number of digits, the sum of digits, and the reverse of a given number entered by the user using a
do-whileloop for iteration.
3. for Loop:
The for loop is normally based on counters i.e. generally a counter is used which is initialized, checked for and incremented/decremented for continuing the loop.
FlowChart:

Syntax:
for (initialization part; Conditional part; increment/decrement)
Example:
#include<stdio.h>#include<conio.h>void main(){int num, x = 0, y = 0, r = 0;printf(“nEnter a number: “);scanf(“%d”, &num); // 123for (; num > 0; x++, num /= 10) {y += num % 10;r = r * 10 + num % 10;}printf(“nDigits = %d”, x);printf(“nSum of Digits = %d”, y);printf(“nReverse of the number is %d”, r);getch();}
Explanation of Program: