ifandelse condition
Operators

Loops

Loops

The loop in c-language  is used to execute a block of code repeatedly. It is often used when the number of iterations is predetermined. If the number of iterations is not predetermined,There are 3 types of loop control statements in C language. 

Functionality of loop

For loops give the following functionality:

  • Initialize the iterator variable using an initial value
  • Check if the iterator has reached its final value
  • Increases the iterator

Do while loop

It is just like while and for loop but the only difference is that the code in its body is executed once before checking the condition.

do{

  statement(s)
}
while(condition);

Now, let’s see the same example of printing ‘Hello World’ 10 times but this time with do…while loop.

#include <stdio.h>
int main()
{
{
    int a = 1 ;
    do
    {
         printf ( "Hello World\n" ) ;
         a ++ ;
    }
    while ( a <= 10 ) ;
}
    return 0;
}

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

While loop

The while loop executes as long as the given logical expression evaluates to true. When expression evaluates to false, the loop stops. The expression is checked at the beginning of each iteration. The execute statements inside the body of the while loop statement are not executed if the expression evaluates to false when entering the loop. It is necessary to update the loop condition inside the loop body to avoid an indefinite loop.

What if someone ask you to print ‘Hello World’ 10 times?

One way is to write the printf statement 10 times. But that is definitely not a good choice if you have to write it 50 times!

So, here comes the while loop.

#include 
int main()
{
    int a = 1;
    while ( a <= 10 )
    {
        printf ( "Hello World\n" );
        a ++;
    }
    return 0;
}

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

For loop in C

int main()
{
    const int max = 5;
    int i;
    for(i = 0; i < max; i++)
        printf("%d ",i);
    return 0;
}

0 1 2 3 4

Lets take another example

int main()
{
  int i;
 
  for(i=0;i<10;i++)
  {
      printf("%d ",i);
  }    
 
}

0 1 2 3 4 5 6 7 8 9

Nested for loop in C

You can put a for loop inside another for loop, which is called a nested for loop. The following example demonstrates how to use a nested for loop to display a pyramid of stars based on the number of levels.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int i, k, levels, space;
    printf("Enter the number of levels in pyramid:");
    scanf("%d",&levels);
 
    space = levels;
 
    for ( i = 1 ; i <= levels ; i++ )
    {
        for ( k = 1 ; k < space ; k++ )
            printf(" ");
        space--;
 
        for ( k = 1 ; k <= 2*i - 1 ; k++ )
            printf("*");
        printf("\n");
    }
    return 0;
}

For example, if you want to display a pyramid with 10 levels, you can see the following output:

*
***
*****
*******
*********
***********
*************
***************
*****************
*******************

See this example to make it clear.

#include <stdio.h>
int main()
{
    int i;
    int j;

    for(i = 12;i<=14;i++)
    {/*outer loop*/

        printf("Table of %d\n",i);

        for(j = 1;j<=10;j++)
        {/*inner loop*/

            printf("%d*%d\t=\t%d\n",i,j,i*j);
        }
    }
    return 0;
}

Table of 12 
12*1    =    12 
12*2    =    24 
12*3    =    36 
12*4    =    48 
12*5    =    60 
12*6    =    72 
12*7    =    84 
12*8    =    96 
12*9    =    108 
12*10    =    120 
Table of 13 
13*1    =    13 
13*2    =    26 
13*3    =    39 
13*4    =    52 
13*5    =    65 
13*6    =    78 
13*7    =    91 
13*8    =    104 
13*9    =    117 
13*10    =    130 
Table of 14 
14*1    =    14 
14*2    =    28 
14*3    =    42 
14*4    =    56 
14*5    =    70 
14*6    =    84 
14*7    =    98 
14*8    =    112 
14*9    =    126 
14*10    =    140

Think about it

What if loop goes on and on?

There may exist some loops that can iterate or occur infinitely.These are called Infinite Loop These loops occur infinitely because their condition is always true.

We can make an infinite loop by leaving its conditional expression empty. When the conditional expression is empty, it is assumed to be true.

Let’s see an example on how to make a for loop infinite.

#include <stdio.h>
int main()
{
    for(;;)
    {
        printf("This is not gonna end!\n");
    }
    return 0;
}

This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!
This is not gonna end!

Comments are closed.