While Loop

A loop statement allows you to execute a statement or block of statements repeatedly. The while loop is used when you want to execute a block of statements repeatedly with checked condition before making an iteration.

This loop executes as long as the given logical expression between parentheses after while is true. When expression is false, execution continues with the statement following the loop block. The expression is checked at the beginning of the loop, so if it is initially false, the loop statement block will not be executed at all. And it is necessary to update loop conditions in loop body to avoid loop forever. If you want to escape loop body when a certain condition meet, you can use break statement.

The general format for a while loop is


while (condition) {
        simple or compound statement (body of the loop);
    }

#include<stdio.h>

main(){
   
    int i = 0;
    while (i<5){
        printf(" the value of i is %d\n", i);
        i = i + 1;
    }
}

the value of i is 0
      the value of i is 1
      the value of i is 2
      the value of i is 3
      the value of i is 4
The reference site for this content is :
http://cprogramminglanguage.net/c-while-loop-statement.aspx
Back