The for loop works well where the number of iterations of the loop is known before the loop is entered. The head of the loop consists of three parts separated by semicolons.
The first is run before the loop is entered. This is usually the initialization of the loop variable.
The second is a test, the loop is exits when this returns false.
The third is a statement to be run every time the loop body is completed. This is usually an increment of the loop counter.
The 3 actions are
“Initialize expression”, “Test Condition expression” and “updation expression”
The expressions are separated by Semi-Colons (;).
for (initialize expression; test condition; updation )
{
Statement-1;
Statement-2;
}
(i)The initialization sets a loop to an initial value. This statement is executed only once.
(ii) The test condition is a relational expression that determines the number of iterations desired or it determines when to exit from the loop.
For loop continues to execute as long as conditional test is satisfied.
When the condition becomes false the control of the program exits from the body of for loop and executes next statements after the body of the loop.
(iii) The updation (increment or decrement operations) decides how to make changes in the loop.
The body of the loop may contain either a single statement or multiple statements.
* for loop can be specified by different ways as shown
Syntax | Output | Remarks |
---|---|---|
for (; ; ) | Infinite to loop | No arguments |
for (a=0; a< =20;) | Infinite loop | “a” is neither increased nor decreased. |
for (a=0; a<=10; a++) printf(“%d”, a) |
Displays value from 1 to 10 | “a” is increased from 0 to 10 curly braces are not necessary default scope of for loop is one statement after loop. |
for (a=10; a>=0; a--) printf(“%d”, a); |
Displays value from 10 to 0 |
“a” is decreased from 10 to 0. |
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( ) ;
for(i = 1; i <=5; i++)
printf(“\n Number: %d it’s Square: %d”, i, i*i);
getch( );
}
Number: 1 it’s Square: 1
Number: 2 it’s Square: 4
Number: 3 it’s Square: 9
Number: 4 it’s Square: 16
Number: 5 it’s Square: 25
We can also use loop within loops. i.e. one for statement within another for statement is allowed in C. In nested for loops one or more for statements are included in the body of the loop. * ANSI C allows up to 15 levels of nesting. Some compilers permit even more.
for( initialize ; test condition ; updation) /* outer loop */
{
for(initialize ; test condition ; updation) /* inner loop */
{
Body of loop;
}
}
# include<stdio.h>
# include<conio.h>
main ( )
{
int x, i, j ;
printf(“How many lines stars (*) should be print f? :”);
scanf(“%d”, &x);
for(i=1; i<=x; i++)
{
for (j=1; j < =i; j++)
{
printf( “*”);
}
printf( “ \n”);
}
getch( );
}
How many lines stars (*) should be print d ? : 5
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
main( )
{
int i, j, x;
printf(“\n Enter Value of x :”);
scanf(“%d”, &x);
for(j=1; j<=x; j++)
{
for(i=1; i<=j; i++)
printf(“%3d”, i);
printf("\n");
}
printf(“\n”);
for(j=x; j>=1; j--)
{
for(i=j; i>=1; i--)
printf(“%3d”, i);
printf("\n");
}
getch( );
}
Enter value of x : 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
The while is an entry-controlled loop statement.
The test condition is evaluated and if the condition is true, then the body of the loop is executed.
The execution process is repeated until the test condition becomes false and the control is transferred out of the loop.
On exit, the program continues with the statement immediately after the body of the loop.
The body of the loop may have one or more statements.
The braces are needed only if the body contains two or more statements.
It’s a good practice to use braces even if the body has only one statement.
The simplest of all the looping structures in C is.
Initialization Expression;
while ( Test Condition)
{
Body of the loop
Updation Expression
}
# include<stdio.h>
# include<conio.h>
main( )
{
int a=1, Sum=0;
while(a<=10)
{
Sum = Sum + a;
a++;
}
printf(“Sum of 1 to 10 numbers is: %d”, sum);
getch( );
}
Sum of 10 numbers is: 55
# include<stdio.h>
# include<conio.h>
main ( )
{
long int n, fact =1;
clrscr( ) ;
printf( “\n Enter the Number:”);
scanf(“%ld”, &n);
while(n>=1)
{
fact = fact*n;
n - - ;
}
printf(“ \n factorial of given number is %d”, fact);
getch( );
}
Enter the Number: 5 /* logic 5 * 4 * 3 * 2 * 1 = 120 */
Factorial of given number is 120
If you pass 1 as a expression in while loop, it will run infinite number of times.
while(1){
//statements
}
To execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true.
The do-while is an exit-controlled loop statement. Because, in do-while, the condition is checked at the end of the loop.
The do-while loop will execute at least one time even if the condition is false initially.
The do-while loop executes until the condition becomes false.
Initialization Expression;
do
{
Body of the loop
Updation Expression;
} while ( Test Condition);
# include<stdio.h>
# include<conio.h>
main( )
{
int n, x=2;
clrscr( );
printf( “Enter the number for testing (prime or not”);
scanf(“%d”, &n);
do
{
if(n%x == 0)
{
printf(“ \n the number %d is not prime”, n);
exit(0);
}
x++;
} while ( x < n);
printf(“ \n the number %d is prime”, n);
getch( ) ;
}
Enter the number for testing (Prime or not): 5
The number 5 is prime.
If you pass 1 as a expression in do while loop, it will run infinite number of times.
The do-while is an exit-controlled loop statement. Because, in do-while, the condition is checked at the end of the loop.
The do-while loop will execute at least one time even if the condition is false initially.
The do-while loop executes until the condition becomes false.
do{
//statements
} while (1);