Loops are used to execute a block of code repeatedly as long as a specified condition is true.
for loop
while loop
do–while loop
Used when the number of iterations is known.
Used when the condition is checked before execution.
Used when the loop must execute at least once.
| Feature | for | while | do–while |
|---|---|---|---|
| Condition check | Before loop | Before loop | After loop |
| Minimum execution | 0 times | 0 times | At least 1 time |
| Use case | Known count | Unknown count | Execute once minimum |
Terminates the loop.
Skips the current iteration.
A loop inside another loop.
Loops repeat execution of statements
for loop is used for fixed iterations
while loop checks condition first
do–while loop executes at least once
break and continue control loop flow
Take quizzes related to this topic and see where you stand!
Start Quiz Now