A while loop is a straightforward loop and is also an entry-control loop. It evaluates the condition before executing the block of the loop. If the condition is found to be true, only then the body of the loop is executed. The loop continues until the condition stated is found to be false.
while loop Flowchart:
Syntax of While Loop.
1 2 3 4 | while(condition) { //block of code to be executed } |
Example of while loop in C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> int main() { //varibale int num = 1; //execution of while loop while (num < 10) { printf("The value of num: %d\n", num); num++; } return 0; } |
The output of while loop in C programming.
The value of num: 1
The value of num: 2
The value of num: 3
The value of num: 4
The value of num: 5
The value of num: 6
The value of num: 7
The value of num: 8
The value of num: 9