C Programming Part 8
C Programming Part 8
1 : while loop
2 : do-while loop
3 : for loop
variable initialization ;
while (condition)
statements ;
The while loop is an entry controlled loop statement, i.e means the condition is evaluated
first and it is true, then the body of the loop is executed. After executing the body of the loop,
the condition is once again evaluated and if it is true, the body is executed once again, the
process of repeated execution of the loop continues until the condition finally becomes false and
the control is transferred out of the loop.
#include<conio.h>
void main( )
C PROGRAMMING Page 71
int x;
x=1;
while(x<=10)
printf("%d\t", x);
x++;
getch();
Output
1 2 3 4 5 6 7 8 9 10
#include<stdio.h>
#include<conio.h>
main()
clrscr();
scanf("%d", &n);
C PROGRAMMING Page 72
while(n!=0)
rem=n%10;
reverse=reverse*10+rem;
n/=10;
getch();
Flowchart
C PROGRAMMING Page 73
do-while loop
Syntax : variable initialization ;
do{
statements ;
}while (condition);
The do-while loop is an exit controlled loop statement The body of the loop are executed first
and then the condition is evaluated. If it is true, then the body of the loop is executed once again.
The process of execution of body of the loop is continued until the condition finally becomes
false and the control is transferred to the statement immediately after the loop. The statements
are always executed at least once.
Flowchart
C PROGRAMMING Page 74
Example : Program to print first ten multiple of 5
#include<stdio.h>
#include<conio.h>
void main()
int a,i;
a=5;
i=1;
do
printf("%d\t",a*i);
i++;
getch();
C PROGRAMMING Page 75
Output
5 10 15 20 25 30 35 40 45 50
Example
main()
int i=0
do
printf("while vs do-while\n");
}while(i= =1);
printf("Out of loop");
Output:
while vs do-while
Out of loop
For Loop:
This is an entry controlled looping statement.
One of the most important features of this loop is that the three actions can be taken at a
time like variable initialization, condition checking and increment/decrement.
The for loop can be more concise and flexible than that of while and do-while loops.
Statements;
C PROGRAMMING Page 76
Example:
#include<stdio.h>
#include<conio.h>
void main( )
int x;
printf("%d\t",x);
getch();
Output
1 2 3 4 5 6 7 8 9 10
1) Here instead of num++, I‟m using num=num+1 which is nothing but same as num++.
2) Initialization part can be skipped from loop as shown below, the counter variable is declared
before the loop itself.
int num=10;
for (;num<20;num++)
Must Note: Although we can skip init part but semicolon (;) before condition is must, without
which you will get compilation error.
C PROGRAMMING Page 77
3) Like initialization, you can also skip the increment part as we did below. In this case
semicolon (;) is must, after condition logic. The increment part is being done in for loop body
itself.
//Code
num++;
4) Below case is also possible, increment in body and init during declaration of counter variable.
int num=10;
for (;num<20;)
//Statements
num++;
5) Counter can be decremented also, In the below example the variable gets decremented each
time the loop runs until the condition num>10 becomes false.
#include <stdio.h>
int main()
scanf("%d", &num);
C PROGRAMMING Page 78
for(count = 1; count <= num; ++count)
sum += count;
return 0;
Output
Sum = 55
#include<stdio.h>
#include<conio.h>
void main(){
int i,fact=1,number;
clrscr();
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
getch();
C PROGRAMMING Page 79
Output:
Enter a number: 5
C PROGRAMMING Page 80