C While Loop
1. What is a while Loop?
-
A
whileloop repeats a block of code as long as a condition is true. -
Syntax:
-
The condition is evaluated first, and if it is true (non-zero), the loop executes.
-
If the condition is false (0) initially, the loop body may never execute.
2. Simple Example
Output:
Note: Incrementing
iis important to eventually make the condition false.
3. Infinite while Loop
-
If the condition never becomes false, the loop will run forever.
-
Use
breakto exit an infinite loop safely.
4. Using while with Input
Sample Input/Output:
5. Key Points
-
Condition is checked first → may skip loop if false initially.
-
Always update loop control variables to avoid infinite loops.
-
Useful for repeated tasks where number of iterations is unknown.
-
Can combine with
breakandcontinuefor more control.
