For Loops in C

The for statement is used repeatedly to execute a single statement or a block of statements, as long as the given condition is TRUE.

How for loop works

  • The initialization statement is executed only once, at the beginning.
  • Then the test condition is evaluated, if the condition happens to be false, the for loop is terminated.
  • If the condition happens to be true, control reaches inside the loop, and statements are executed and at the same time update expression is updated.
  • Again, the test expression is evaluated.

This process goes on until the test expression is false.

Simple example:

1) To print “hello world” 10 times

#include <stdio.h>

int main()
{
  for (int i = 0; i < 10; i++)
  {
    printf("\n Hello world");
  }
   return 0;
 }

2) To print a table of 2

#include <stdio.h>

int main()
{
    int i = 2;
    for (; i <22; i+=2)
    {
       printf("\n %d", i);
    }
    return 0;
}

In this example, we can see that the variable “i” has been initialized outside the loop.


3) To print 1 to 10

In this the updated expression is missing, which can be given even later.

#include <stdio.h>

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

4)In this we can see multiple initialization and update expression

#include <stdio.h>

int main()
{
     for (int i = 1, j=1; i <= 10, j < 10; i++, j+=2) 
     {
          printf("\n i value is : %d and j value is: %d", i,j);
     }
  return 0;
}

5)This example illustrates Infinite for loop

#include<stdio.h>
int main()
{
    int i =1;
    for(;;) 
    {
        printf("\n Hello World");
        if ( i==10) {
            break;
        }
        i++;
    }
    return 0;
}

6)Nested For loop

#include<stdio.h>
int main()
{
    int i, j;
    for(j=1; j<=5; j++)
      {
         for (i=1; i<=j; i++)
            printf ("%5d", i);
         printf ("\n\n");
       }
    return 0;
}

OUTPUT: 1
    1    2                                                                                                                                                                              1    2    3                                                                                                                                                                         1    2    3    4                                                                                                                                                                    1    2    3    4    5                                                                                                                                                          
Note:

Do not give a semicolon(;) at the end of for loop, there will be no Compilation error but the control will not enter the loop. It is very similar to the body-less loop.

Relevant Posts:



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: