When a single statement or a group of statements will be executed again and again in a program (in an iterative way), then such type processing is called loop. These statements are also called Iterative Structure or Program Loop. This allows a sequence of program statements to be executed several times, either a specified number of times or until a particular condition is satisfied. It consists of an entry point that may include the initialization of loop variables, a loop continuation condition, a loop body, and an exit point.
Loop is divided into two parts:
- Body of the loop
- Control of loop
The looping statements used in C-language are:
- while statement or while loop
- do statement or do loop
- for statement or for loop
- Nested for loop statement
The loop continuation condition may be tested before the loop body is executed as in the case of while and for loops. In such a case, the loop is referred to as a pre-test loop. The case in which the condition is tested after the execution of the loop body, as in the case of do-while loop, such a loop is called a post-test loop.
while Statement or while loop
While statement or while loop is an entry control loop. In this, first of all, the condition is checked and if it is true, then a group of statements or body of the loop is executed. It will execute again and again till condition becomes false.
The general syntax is:
while (test condition) { block of statements; } statements-x;
Example Program:
/*The easiest way to use while statement or while loop*/ #include <stdio.h> #include <conio.h> void main() { int i,s=0; clrscr(); i=1; while(i<=10) { printf("\n I=%d",i); s = s + i; i++; } getch(); }
Output is as follows:
I=1 I=2 I=3 I=4 I=5 I=6 I=7 I=8 I=9 I=10
do Statement or do loop
Do statement is exit control loop. It is also called do-while statement. In this statement, first body of the loop is executed and then the condition is checked. If condition is true, then the body of the loop is executed. When condition becomes false, then it will exit from the loop.
The syntax of do-loop is as follow:
do { block of statements; } while (condition); statement-x;
Example Program:
/*Write a Program to print the 1 To 10 Numbers using do loop*/ #include <stdio.h> #include <conio.h> void main() { int i,n=10; clrscr(); do { printf("I=%d",i); i++; } while(i<=n); getch(); }
Output is as follows:
I=1 I=2 I=3 I=4 I=5 I=6 I=7 I=8 I=9 I=10
for Statement or for loop
It is a looping statement, which repeats again and again till it satisfies the defined condition. It is one step loop, which initializes, checks the condition, and increments/decrements the step in the loop in a single statement.
The general syntax is as:
for(initial value; test condition; increment/decrement) { body of the loop; } statement-x;
Example Program:
/*Write a Program to print the 1 To 10 Numbers using for loop*/ #include <stdio.h> #include <conio.h> void main() { int i; clrscr(); for(i=1;i<=10;i++) //initial value;test condition;increment/decrement { printf("\n I=%d",i); } getch(); }
Output is as follows:
I=1 I=2 I=3 I=4 I=5 I=6 I=7 I=8 I=9 I=10
Nested for loop statement
When a for statment is executed within another for statement, then it is called nested for statement. We can apply number of nested for statement in C-Language.
The general syntax is as :
for(initial value1; test condition1; increment/decrement1) { for(initial value2; test condition2; increment/decrement2) { inner-body of the loop; } outer-body of the loop; statement-x; }
Example Program:
/*Write a Program to print the below output using nested for loop * ** *** **** ***** */ #include <stdio.h> #include <conio.h> void main() { int r,c; clrscr(); for(r=1;r<=5;r++) { for(c=1;c<=r;c++) { printf("*"); } printf("\n"); } getch(); }
Output is as follows:
* ** *** **** *****