While loop in C in C Language
Updated on May 31, 2025 | By Learnzy Academy
A while loop in C is a control structure used to repeat a block of code as long as a given condition is true. It is commonly used when the number of iterations is not known beforehand.
In a while loop, the condition is evaluated before the code block is executed. If the condition is true, the loop body runs. After each iteration, the condition is checked again. The loop continues until the condition becomes false.
Because the condition is checked first, it's possible that the loop body may never execute if the condition is false from the beginning.
while (condition) {
// code to be executed repeatedly
}
Use Cases of While Loop:--
- Use
whilewhen you want to repeat something but you don't know exactly how many times in advance. - Often used to read data from a file or stream until there’s nothing left to read.
- Used in games or simulations to run continuously until the game ends.
- You don't know how many times the loop will run.
- The loop depends on something changing inside the loop.
Click here to download practice questions on
While loop in C