while loop in C Programming Language

Rumman Ansari   Software Engineer   2023-03-27   12074 Share
☰ Table of Contents

Table of Content:


Syntax while loop

The while loop is C’s most fundamental loop statement. while loop executes statements repeatedly while the condition is true.

while loop in c programming language

while(expression)
{
  ..............
  program statement
  ..............
}

Expresion or loop continuation condition

The expression specified inside the parentheses is evaluated. If the result of the expression evaluation is true. The program statement that immediately follows is executed. After execution of this statement (or statements,if enclosed in braces), expression is again evaluated. If the result of the evaluation is true, the program statement is again executed. This process continues until expression finally evaluates false, at which point the loop is terminated. Execution of the program then continues with the statement that follows program statement.

The condition may be any expression, and true is any nonzero value.

program statement or Loop body:

The program statement is either an empty statement, a single statement, or a block of statements.

Braces { }:

The curly braces are unnecessary if only a single statement is being repeated.


Extra touch in syntax
i = initialValue; // Initialize loop control variable
while (i < endValue) {
// Loop body
...
i++; // Adjust loop control variable
}
 

Program

As an example of its use,the following program sets up a while loop,which merely counts from 1 to 5.

 // This program introduces the while statement
#include
void main()
{
 
int count = 1; // initialization part

while(count <= 5 ) { // expresson part or conditional part
 printf("%i \n",count);
 ++count;   // increment part
 }
 
 
}
 

Output

1
2
3
4
5
Press any key to continue . . .

Explanation

The program initially sets the value of count to 1; execution of the while loop then begins. Because the value of count is less than or equal to 5, the statement that immediately follows is executed. The braces define both the print statement and the statement that increments count as the body of the while loop. From the output of the program, you can see that this loop is executed five times or until the value of count reaches 5 .

While Loop with no body

 
#include
void main()
{
    int i, j;

    i = 10;
    j = 20;
    printf("%d\n",i);
    printf("%d",j);

    while(i < j);  // no body in this loop
 
}
 

Output

10
20

Another Example of while loop:

 
#include
void main()
{
int n=1;
    while(n<=10){
        printf("%d\n",n);
    n++;
    }
}
 

Output

1
2
3
4
5
6
7
8
9
10
Press any key to continue . . .
 

Single Statement Inside while Loop Body

Single Statement is a part of While Loop as there is no opening and closing curly braces. If we want to write multiple statements as a part of while loop body then we have to put all the statements in a block. i.e the { }

while(true)   
//code to be executed  or statement(single); 
  

Example of while loop:

 
#include 
void main()
{
 int count = 1;
     while (count <= 11)
         printf("Number Count : %d\n",count++);
}
 

Output

Number Count : 1
Number Count : 2
Number Count : 3
Number Count : 4
Number Count : 5
Number Count : 6
Number Count : 7
Number Count : 8
Number Count : 9
Number Count : 10
Number Count : 11
Press any key to continue . . .
 

Note: Make sure that the loop-continuation-condition eventually becomes false so that the loop will terminate. A common programming error involves infinite loops (i. e., the loop runs forever). If your program takes an unusually long time to run and does not stop, it may have an infinite loop. If you are running the program from the command window, press CTRL+C to stop it.

C Infinite While Loop

If you pass true in the while loop, it will be an infinitive while loop.

while(true){  
//code to be executed  or statement(s); 
}  

Example 1:

 
#include  
void main()
{
 while(1==1){
      printf("infinite while loop \n");
    }
}
 

Output

infinite while loop
infinite while loop
infinite while loop
infinite while loop
infinite while loop
infinite while loop
infinite while loop
..................
..................
..................
..................
upto infinite
 

Example 2:

 
#include  
void main()
{
 while(22 > 1){
      printf("infinite while loop \n");
    }
}
 

Output

infinite while loop
infinite while loop
infinite while loop
infinite while loop
infinite while loop
infinite while loop
infinite while loop
..................
..................
..................
..................
infinite time it will print like this
 

Common Error in Java Program

off-by-one error

Programmers often make the mistake of executing a loop one more or less time. This is commonly known as the off-by-one error.Example:

 
 
#include
void main()
{
int number = 0;
  while (number <= 100 ) {
	 printf(" Hello Dhinchak Pooja! \t Selfie mayne la liya!! - %d\n",number);
     number++;
  }
} 
 

Output

 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 0
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 1
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 2
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 3
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 4
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 5
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 6
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 7
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 8
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 9
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 10
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 11
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 12
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 13
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 14
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 15
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 16
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 17
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 18
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 19
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 20
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 21
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 22
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 23
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 24
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 25
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 26
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 27
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 28
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 29
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 30
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 31
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 32
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 33
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 34
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 35
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 36
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 37
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 38
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 39
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 40
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 41
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 42
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 43
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 44
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 45
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 46
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 47
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 48
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 49
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 50
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 51
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 52
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 53
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 54
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 55
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 56
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 57
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 58
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 59
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 60
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 61
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 62
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 63
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 64
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 65
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 66
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 67
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 68
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 69
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 70
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 71
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 72
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 73
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 74
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 75
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 76
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 77
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 78
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 79
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 80
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 81
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 82
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 83
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 84
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 85
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 86
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 87
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 88
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 89
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 90
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 91
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 92
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 93
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 94
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 95
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 96
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 97
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 98
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 99
 Hello Dhinchak Pooja!   Selfie mayne la liya!! - 100
Press any key to continue . . .
 

Explanation:

The following while loop displays Hello Dhinchak Pooja!     Selfie mayne la liya!! 101 times rather than 100 times. Please see the out it is started form 0 to 100. i.e 101 times. The error lies in the condition, which should be number < 100 rather thannumber <= 100