C While Loop

In this tutorial, you will learn about the while loop in the C programming language with the help of examples.


In C language, while loops are used when you do not know how many times a particular loop will execute. For loops are used when you know previously how many times a loop will execute. While loops are common where the program runs in an infinite loop until the user terminates it or while a specific input device will keep sending it input, the while statement will continue to execute.

C While Loop Syntax

The syntax of the while loop in C language is as follows.

Initialization of the expression
while (test or exit condition expression)
{
// statements which will be executed when the condition is true
update_expression;
}

On breaking down the different parts of a while loop we will get the following:

  • Initialization Expression: This expression is used to initialize the while loop with an initial value by either declaring and initializing a variable there or only by initializing an existing variable at that point. It is declared outside the body of the loop. Eg. int i=6; or j=k; // Provided both j and k have been declared and k has been initialized.
  • Test or Exit Condition: This expression will keep on getting executed until the condition is false and then the program flow will move out of the while loop. If the condition is true, it will move into the block and execute the statements inside the while loop. Eg. i<10;
  • Update Expression: This expression will get executed after the statements in the while loop have been executed and before the while loop condition is evaluated. It will execute as many times as the while loop statement will get executed and it can be mentioned anywhere in the while block. Eg. i++;

Flowchart of While Loop in C

C While Loop

The Structure of While Loop Execution

The while loop execution follows the following pattern of code execution which are as follows :

  1. Execution begins by initialization
  2. The execution moves onto the condition of the while loop.
    1. If the condition is true, the flow moves onto the body.
    2. If the condition is false, the flow moves to the outside of the body of the loop.
  3. The loop block and the update statement are executed.
  4. After the update, the control moves to the condition statement in Step no 2.
  5. The above statements from steps 4 to 2 are repeated till the condition is false. Once false it exits from the block.

C While Loop Example

#include<stdio.h> 
int main(){ 
// Writing a while loop to print Hello World 4 times
int i=1;
while ( i<5 ){
printf("Hello World\n");
i++;
} 
return 0; 
}

Output

Hello World
Hello World
Hello World
Hello World

Nested While Loop in C

When one while loop is written another while loop is called nested while loop. The following example will explain how a nested for loop works.

#include<stdio.h> 
int main(){ 
// Nested while loop example
int j,i=1;
while ( i<=5 ){ // first while loop
j=i;
while ( j<=5 ) { // second while loop
printf("%d\t",j);
j++;
}
printf("\n");
i++;
} 
return 0; 
}

Output

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Infinite While Loop in C

If the expression passed in the while loop never becomes false then the loop will run an infinite number of times. An infinite While loop can be due to a logical error or can be deliberately placed for a particular reason like waiting for the program to receive an input from a source and then continue execution. It can be achieved by either removing the exit condition or by not providing an update statement or even both.

Example 1: Infinite While Loop

#include<stdio.h> 
int main(){ 
int i=1;
while ( i<5 ){ // No update statement
printf("Hello World\n");
} 
return 0; 
}

Output

Hello World
Hello World
Hello World
Ctrl + C

Example 2: Infinite While Loop

#include<stdio.h> 
int main(){ 
int i=1;
while ( i>0 ){ // Condition is always true
printf("Hello World\n");
i++;
} 
return 0; 
}

Output

Hello World
Hello World
Hello World
Ctrl + C

Please get connected & share!

Advertisement