Google News
logo
Loop Control Statements In C Language
Loop : A loop is defined as a block of statements which are repeatedly executed for certain number of times.
In other words it iterates a code or group of code many times.

Why use loops in c language : Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop. For example: Suppose that you have to print table of 2 then you need to write 10 lines of code, by using loop statements you can do it by 2 or 3 lines of code only.

(1) for Loop :

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 (;).

  Syntax :
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.

Free Time Learning

* 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.
  Program : Print the first five numbers starting from one together with their squares.
#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( );
}
Output :

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

Nested “for” loop :

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.

  Syntax :
for( initialize ;  test condition ;  updation)   /* outer loop */
{ 
 for(initialize ;  test condition ;  updation) /* inner loop */
   { 
    Body of loop; 
   }
} 
  Program : The following program is an example of nested for 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( );
  }
Output :

How many lines stars (*) should be print d ? : 5
*
* *
* * *
* * * *
* * * * *

  Program : The following program is an example of nested for loop.
#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( );
 }
Output :

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

(2) The “while” loop :

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.

  Syntax :
Initialization Expression;  
   while ( Test Condition)  
{ 
   Body of the loop 
   Updation Expression
}  
Free Time Learning
  Program : To add 10 consecutive numbers starting from 1. Use the while loop.
# 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( );
} 
Output :

Sum of 10 numbers is: 55

  Program : To calculate factorial of a given number use while loop.
# 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( );
} 
Output :

Enter the Number: 5 /* logic 5 * 4 * 3 * 2 * 1 = 120 */
Factorial of given number is 120

Infinitive while loop

If you pass 1 as a expression in while loop, it will run infinite number of times.

  Syntax :
while(1){
//statements
  }
(3) The “do-while“ loop :

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.

  Syntax :
Initialization Expression;
   do 
     { 
   Body of the loop 
   Updation Expression; 
   } while ( Test Condition); 
Free Time Learning
  Program : To check whether the given number is prime or not.
# 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( ) ; 
} 
Output :

Enter the number for testing (Prime or not): 5
The number 5 is prime.

Infinitive do while loop :

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.

  Syntax :
do{
     //statements
    } while (1);