C TutorialLesson 15Free preview
C While Loop
This lesson is part of C · All levels
Summary
While the loop runs the block code while the condition inside the cage is still valid. The condition is checked before each iteration, so if the initial condition is wrong then the block is never run at all. It's important that there are changes inside the loop that eventually make the condition wrong, otherwise the program will be trapped in an infinite loop. While it is suitable for use when the number of iterations is not known from the beginning.
Key points
- While checking the condition before running each iteration (pre-test loop)
- The condition must eventually be wrong to stop the loop.
- No change in the condition causes the infinite loop
- While it suits when the number of repetitions is unknown at the beginning
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.