Skip to main content

12. While and For Loops

 

While Loop in C

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. The while loop is mostly used in the case where the number of iterations is not known in advance.

while(condition){
    // Code to be executed
}

Program to print table for the given number using while loop in C

#include <stdio.h>

int main()
{
   int i = 1;
   int j;
   printf("Enter a number : ");
   scanf("%d", &j);
   while (i <= 10){
      printf("%d x %d = %d \n", j, i, i*j);
      i++;
   }
   return 0;
}

Output :

Enter a number : 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

#include <stdio.h>

int main()
{
   int a = 1;
   while (a += 2, a <= 10){
      printf("%d ", a);
   }
   printf("%d", a);
   return 0;
}

Output :

3 5 7 9 11

Infinitive while loop in C

If the expression passed in while loop results in any non-zero value then the loop will run the infinite number of times, i.e. while() or while(1).

For loop in C

The for loop is also called as a pre-tested loop. The for loop in C language is used to iterate the statements or a part of program several times. It is better to use for loop if the number of iteration is known in advance.

for(Expression 1Expression 2Expression 3){
    // Code to be executed
}

  • Expression 1 represents the initialization of the loop variable.
    We can initialize more than one variable in Expression 1.
    Expression 1 is optional.
  • Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, then the loop is terminated.
    It can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.
    It is optional.
    Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the variables as well as update the loop variable in expression 2 itself.
    We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true and zero is false by default.
  • Expression 3 is used to update the loop variable.
    We can update more than one variable at the same time.
    It is optional.

Program to print table for the given number using for loop in C

#include <stdio.h>

int main()
{
   int x = 1;
   int y;
   printf("Enter a number : ");
   scanf("%d", &y);
   for (x = 1; x <= 10; x++){
      printf("%d x %d = %d \n", y, x, x*y);
   }
   return 0;
}

Enter a number : 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

#include <stdio.h>

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

Output :

0 2
1 4
2 6
3 8
4 10

Infinitive for loop in C

To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop, i.e. for(;;). This will work as an infinite for loop.

Comments