Open In App

while Loop in C

Last Updated : 20 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The while loop in C allows a block of code to be executed repeatedly as long as a given condition remains true. It is often used when we want to repeat a block of code till some condition is satisfied.

Example:

C
#include <stdio.h>

int main()  {
    int i = 1;

    // Condition for the loop
    while (i <= 5) {   
        printf("GfG\n");   
      
        // Increment i after each iteration
        i++;  
    }

    return 0;
}

Output
GfG
GfG
GfG
GfG
GfG

Explanation: The above loop prints the text “GfG” till the given condition is true i.e. i is less than 5. In each execution of the loop’s statement, it increments i till is less than 5.

Syntax of while Loop

C
while (condition) {
    // Body
    updation
}

where,

  • Initialization: In this part, we initialize the loop variable to some initial value. Initialization is not part of while loop syntax but it is essential when we are using some variable in the test expression
  • Conditional: This is one of the most crucial part as it decides whether the block in the while loop code will execute. The while loop body will be executed if and only the test condition defined in the conditional statement is true.
  • Body: It is the actual set of statements that will be executed till the specified condition is true.
  • Updation: It is an expression that updates the value of the loop variable in each iteration. It is also not part of the syntax, but we have to define it explicitly in the body of the loop.

Working of while Loop

Let's understand the working of while loop in C using the flowchart given below:

C While Loop
flowchart for while loop

We can understand the working of the while loop by looking at the above flowchart:

  • STEP 1: When the program first comes to the loop, the test condition will be evaluated.
  • STEP 2A: If the test condition is false, the body of the loop will be skipped program will continue.
  • STEP 2B: If the expression evaluates to true, the body of the loop will be executed.
  • STEP 3:  After executing the body, the program control will go to STEP 1. This process will continue till the test expression is true.

Examples of while Loop

The below examples show how to use a while loop in a C program:

Sum of First N Natural Numbers using While loop

C
#include <stdio.h>

int main() {
    int sum = 0, i = 1;

    while (i <= 10) {
      
        // Add the current value of i to sum
        sum += i;
      
        // Increment i
        i++;       
    }

    printf("%d", sum);
    return 0;
}

Output
55
C
#include <stdio.h>

int main() {

  	// Initialize outer loop counter	
    int i = 1;

    // Outer while loop to print a multiplication
    // table for all numbers up to 5
    while (i <= 5) {

      	// Initialize inner loop counter for each row
        int j = 1;

        // Inner while loop to print each value in table
        while (j <= 5) {
            printf("%d ", i * j);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

Output
1 2 3 4 5 
2 4 6 8 10 
3 6 9 12 15 
4 8 12 16 20 
5 10 15 20 25 

Explanation: In the above program, one while loop is nested inside anther while loop to print each value in the table. This is called nesting of loops and why can nest as many while loops as we want in in C.

Infinite while loop

An infinite while loop is created when the given condition always remains true. It is generally encountered in programs where, the condition variable is not correctly updated or has some logic error.

C
#include <stdio.h>

int main() {
  
    // Infinite loop condition (1 is always true)
    while (1) {   
        printf("GfG\n");
    }

    // This line is never reached due to infinite loop
    return 0;  
}


Output

GfG
GfG
.
.
infinite

As seen in the above example, the loop will continue till infinite because the loop variable will always remain the same resulting in the condition that is always true.

Important Points about while loop

  • It is an entry-controlled loop.
  • It runs the block of statements till the condition is satisfied, once the condition is not satisfied it will terminate.
  • Its workflow is firstly it checks the condition and then executes the body. Hence, a type of pre-tested loop.
  • This loop is generally preferred over for loop when the number of iterations is unknown.

Next Article
Article Tags :

Similar Reads