Definition:
The while loop is an entry-controlled loop in C programming. It repeatedly executes a block of code as long as the specified condition is true. When the condition becomes false, the loop terminates and the control moves to the next statement after the loop.
How It Works?
while loop checks the specified condition.Syntax:
Initialization;
while(condition)
{
// Statements
Update;
}
#include <stdio.h> #include <conio.h> main() { int k = 1; clrscr(); while(k <= 10) { printf("\n%d", k); k = k + 1; } getch(); }
while loop to print numbers from 1 to 10.while loop for repeating a statement multiple times until a condition becomes false.while condition to check whether the number is less than or equal to 20. Increase the value by 1 inside the loop.#include <stdio.h> #include <conio.h> main() { int k = 10; clrscr(); while(k <= 20) { printf("\n%d", k); k = k + 1; } getch(); }
k initialized to 1. The while loop checks whether k is less than or equal to 10. If the condition is true, the current value of k is displayed and then increased by 1. The loop continues until the value reaches 10.