C# TutorialLesson 17Free preview
C# While Loop
This lesson is part of C# · All levels
Summary
A while iteration repeats a block of code while its condition is still true, so that the number of iterations can only be known as the program runs. The do while variants check the condition at the end so that the block is run at least once. Make sure the control variable always changes within the loop so that there is no infinite loop.
Key points
- While (condition) checks the condition before each iteration; when false at the beginning, the block is never executed.
- Do while running the block first just check, so the block is at least once executed.
- The control variable must be updated within the loop to avoid an infinite loop.
- While it's perfectly suited when the number of iterations depends on the constantly changing conditions.
- Placing value additions before or after printing will change the end result of the program.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.