While loop
The while
loop is one of the simplest and most commonly used control structures in programming. Its syntax is as follows:
Understanding the While Loop
In this context, the condition is any expression that evaluates to a Boolean result, meaning it can be either true
or false
. The loop will continue to execute as long as the condition remains true
. Once the condition becomes false
, the loop terminates. It's important to note that if the condition is initially false
, the loop body will not execute at all. This characteristic distinguishes the while
loop from the do ... while
loop, which guarantees at least one execution of the loop body.
Example: Calculating the Average of a Series of Numbers
Let’s look at a practical example where we use a while
loop to calculate the average of a series of numbers.
Code Explanation
- Lines 14, 16: The
finished
Boolean variable acts as the condition for the loop. The loop will continue to run as long asfinished
is not true. - Line 22: When the user enters
0
,finished
is set to true, terminating the loop. If the input is non-zero, the program increments thecount
and adds the input value tosum
. - Line 30: Once the loop ends, the average is calculated using
sum
divided bycount
. A check is included to ensure that at least one valid number was entered to avoid division by zero.
Note
If the condition is false
when the loop is first encountered, the loop will not execute at all. This property makes the while
loop a top-tested loop, as the condition is evaluated before each iteration.
Alternative Exit Conditions
An alternative approach could involve asking the user for the total number of values to be averaged. In this case, the loop would need a counter variable to track how many numbers have been entered, allowing it to terminate gracefully once the specified number is reached.