Introducing while loop in C. Simple program with detailed explanation.
(Similar post on while loop is here.)
(For Part 2 of this tutorial, see here.)













Introducing while Loop in C
Loops are used to execute a specific piece of code multiple times.
while (some condition){ // block of code}
As long as the condition is true, the block of code within the while loop is repeatedly executed.
Q. Print “Good Morning” three times.
Solution
<stdio.h>int main(){ int i = 1; while (i <= 3) { printf("Good Morning\n"); i++; } printf("Out of the loop now\n"); return 0;}
Output
Good MorningGood MorningGood MorningOut of the loop now
For a detailed explanation of the above program, please refer to the images above.
Leave a comment