C++ PROGRAMMING: HANDLING REPETITIONS
WHILE LOOP STATEMENT
In this section we will discuss while loop. As discussed earlier, loops are used for executing a block of program
statements repeatedly until the given loop condition returns false.
After doing this section, you are expected to:
1. Identify the flow of control using while loop statement
2. Use the while loop statement to formulate loop statements
3. Create a program using the while loop statement to satisfy certain problem
INTRODUCTION
Condition-Controlled Loops
It is not always possible to know in advance how many times a particular set of operations should be repeated. In such
cases, we need a loop that reacts to changing conditions. A condition-controlled loop is a loop that tests a condition to
determine whether a block of enclosed statements should be executed repeatedly.
One type of condition-controlled loop evaluates an expression prior to entering the loop. This is called the pre-test loop. This
condition determines whether program flow can proceed into the loop body. If the condition is satisfied, the statement block inside the
loop structure executed. Once the last statement is executed, control is transferred back to the beginning of the loop where the pre-test
is evaluated again. As long as the expression remains true, the execution of the statement block inside the loop will be repeated.
If the condition is not satisfied, then the loop execution ends. The loop body is disregarded and control is transferred to the next
executable statement after the loop structure. If at the very start, the result of the pre-test condition is already false, then the loop body
won’t even be executed.
DISCUSSION
while Loop
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly
until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the
program after while loop.
Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so
that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of
while loop otherwise the loop would execute indefinitely.
Syntax of while loop:
while(condition)
WHILE LOOP STATEMENT
In this section we will discuss while loop. As discussed earlier, loops are used for executing a block of program
statements repeatedly until the given loop condition returns false.
After doing this section, you are expected to:
1. Identify the flow of control using while loop statement
2. Use the while loop statement to formulate loop statements
3. Create a program using the while loop statement to satisfy certain problem
INTRODUCTION
Condition-Controlled Loops
It is not always possible to know in advance how many times a particular set of operations should be repeated. In such
cases, we need a loop that reacts to changing conditions. A condition-controlled loop is a loop that tests a condition to
determine whether a block of enclosed statements should be executed repeatedly.
One type of condition-controlled loop evaluates an expression prior to entering the loop. This is called the pre-test loop. This
condition determines whether program flow can proceed into the loop body. If the condition is satisfied, the statement block inside the
loop structure executed. Once the last statement is executed, control is transferred back to the beginning of the loop where the pre-test
is evaluated again. As long as the expression remains true, the execution of the statement block inside the loop will be repeated.
If the condition is not satisfied, then the loop execution ends. The loop body is disregarded and control is transferred to the next
executable statement after the loop structure. If at the very start, the result of the pre-test condition is already false, then the loop body
won’t even be executed.
DISCUSSION
while Loop
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly
until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the
program after while loop.
Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so
that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of
while loop otherwise the loop would execute indefinitely.
Syntax of while loop:
while(condition)