Do While loop does not execute if condition is false?

Syntax

Do While loop does not execute if condition is false?

Details about syntax

while (true/false expression)

The true/false expression in the while statement can be anything that evaluates to true or false. Just like if statements, 0 is treated as false and any non 0 number is treated as true. Char values can also be used as they are treated as integers.

Statements following while (true/false expression)

Follow the while(true/false expression) with a single statement or a block of code. The single statement or code block is repeatedly executed while the condition is true.

Example
// execute single statement while (loopCount) cout << "in while loop " << loopCount-- << endl; while (loopCount) { // execute multiple statements // ... }

Stopping the looping

Do While loop does not execute if condition is false?
The while statement will repeatedly execute the loop statement or code block while the true/false expression is true. To stop the looping, something needs to happen to change the true/false expression to false.

There are two possible places to modify variables used in the true/false expression, within the code executed during the loop or within the true/false expression itself.

Stopping a loop by modifying variables with the code executed during the loop

In this case, the operand or operands of the while statement’s true/false expression are modified in the while statement’s code block.

Example 1

while (count > 0) { // do some work // ... // change the value of count count = count - 1; }

Example 2

In this example, the user inputs a new value for testScore during every loop. The while statement will loop until the user inputs a correct value within 0 and 100.

int testScore; cout << "Enter test score (0 to 100): "; cin >> testScore; while (testScore < 0 || testScore > 100) // testScore is not valid { cout << "Invalid test score entered. Re-enter score: "; // Get a new value for testScore every time the loop executes cin >> testScore; }

Note about input validation

Example 2 illustrates writing a program with input validation. A fully developed and complete program should include input validation. Never assume that inputs are always correct.

Stopping looping by modifying variables during evaluation of the while true/false expression

There are several ways to change the while true/false expression when the while true/false expression is evaluated. The most common way is to use the increment and decrement operators.

Increment and decrement operators

It is very common when programming to want to add or subtract one from a variable.  Therefore, there are two handy shortcut operators.

OperatorNameActionPronunciation
++ increment operator add one plus plus
-- decrement operator minus one minus minus
  • The increment and decrement operators are unary operators in that they use a single operand.
  • The increment and decrement operators work on numeric values.
  • The operand must be a non-constant lvalue such as a variable. You can’t apply ++ and -- to literals.

Prefix and postfix mode

You can place the increment and decrement operators before (prefix mode) or after (postfix mode) the operand.

ExpressionTypePronunciation
someVariable++ postfix someVariable plus plus
++someVariable prefix plus plus someVariable
someVariable-- postfix someVariable minus minus
--someVariable prefix minus minus someVariable

Effect of prefix and postfix mode in simple expressions

In simple expression, prefix and postfix modes act the same.

// both are equivalent to x = x + 1; x++; ++x; // both are equivalent to x = x - 1; x--; --x;

Effect of prefix and postfix mode in statements that do more than increment or decrement

In statements that do more than increment or decrement a variable, the prefix and postfix modes result in very distinct differences.

  • If you use prefix mode, the variable is incremented or decremented before the expression is evaluated.
  • If you use postfix mode, the variable is incremented or decremented after the expression is evaluated.
x = 5; // postfix, x incremented after addition y = 10 + x++;
  • At the end of this code, x = ? and y = ?

    x = 6
    y = 15

x = 5; // prefix, x incremented before addition y = 10 + ++x;
  • At the end of this code, x = ? and y = ?

    x = 6
    y = 16

Stopping looping by use decrement in while true/false expression

In the following example, we want the while statement to loop five times and stop. By using numberOfInputs-- in the while true/false expression, the while statement counts down from 4 to 0. When 0 is reached, the while expression is equal to while(0) so the looping stops.

Do While loop does not execute if condition is false?

Counters

The above example illustrates what is called a counter. A counter is a variable that is incremented or decremented each time the program executes the loop. The counter variable numberOfInputs was decremented each time the program looped through the while statement.

The one thing that can sometimes trip people up on counter variables is that they don’t use the proper relational operators for starting or ending counts at 0 versus 1.

It may seem strange to count starting from 0 or ending at 0 but as you will see in later chapters, the first element in lists of objects or values such as in arrays is at index value 0 so C++ programmers become very used to counting from 0 or ending at 0.

Example of counting up using a counter

The table below illustrates the proper relational operators to use when counting up from 0 versus 1.

The table uses a counter variable named countVar.

If countVar is incremented by one every time the loop executes and you want to loop x times, use the following rules.

countVar starting atProper relational operatorRelational expression for looping x timesValues of countVar during looping
0 < while(countVar < x) 0, 1, 2 ... , x-1
1 <= while(countVar <= x) 1, 2, 3, ..., x
Example of counting down using a counter

The table below illustrates the proper relational operators to use when counting down to 0 versus 1.

As in the above table, the table uses a counter variable named countVar.

When countVar is decremented by one every time the loop executes and you want to loop x times, use the following rules.

Starting value of countVarCount ending atProper relational operatorRelational expression for looping x timesValues of countVar during looping
x 1 > 0, 1, 2 ... , x-1 x, x-1, ..., 1
x-1 0 >= while(countVar >= 0) x-1, x-2, ..., 0

Note that you can come up some other relational expressions to add to these tables. The main point here is to be aware of the impact of starting or stopping at 0 versus 1 and that you need to be careful in creating the relational expression so you don’t loop one too many or one too few times.

Here is quick example of an incrementing counter.

int numberOfInputs = 5; int count = 0; // use < because count starts at 0 while (count < 5) { cout << "Current value of count: " << count << endl; count++; }

Infinite loops

Do While loop does not execute if condition is false?
An infinite loop is one that starts and never stops. This happens when the true/false expression never returns a false value. While there can be some cases where you might want this to happen, most often an infinite loop is the result of a bug in your program’s logic.

helpful hint if your program will not stop

Use your keyboard and press CTRL and then c so you are holding CTRL and c down at the same time (called Control-C).

Do not let semicolons cause endless or infinite loops

When you first start working with while statements, you might accidentally place a semicolon after the “while(true/false expression)” part of the statement such as shown below. The semicolon results in a null statement, a statement that does nothing. In the example below, because of the null statement, the loopCount is never changed so the looping never stops.

Do While loop does not execute if condition is false?

While is a pretest loop

Because the while’s true/false expression is evaluated before the looping starts, it is a pretest loop. With pretest loops such as the while loop, the program may never execute the loop statements. If the initial evaluation of the while statement’s true/false expression is false, the program skips all statements of the loop and continues execution after the while statement.

Do While loop does not execute if condition is false?

Letting users control the number of loops

There are several ways to allow users to control the number of loops.

Example 1

The user is asked how many test results they want to enter and then the program loops the required number of times.

Do While loop does not execute if condition is false?

Example 2

In this example, the user is asked to guess the digit the program has chosen. The program loops until they guess correctly but the user can stop the looping at any point by entering a non-digit value.

Do While loop does not execute if condition is false?

Video lectures

More Repetitive Execution (while loops and do-while loops) - by Jennifer Parham-Mocello

The While Loop (demo) - by Jennifer Parham-Mocello

Common Mistakes with While Loops (demo) - by Jennifer Parham-Mocello

What happens when the condition is false in do

If the expression is false, the loop terminates and control transfers to the statement following the do-while loop.

Do

while loop is executed once before the condition is checked. Its syntax is: do { // body of loop; } while (condition); ... If the condition evaluates to true , the body of the loop inside the do statement is executed again.

Does a while loop occur if the condition is true or false?

🔹 Purpose and Use Cases for While Loops They are used to repeat a sequence of statements an unknown number of times. This type of loop runs while a given condition is True and it only stops when the condition becomes False .

How many times do

When condition becomes false, control passed to the first statement that follows body of the loop. While loop will be executed atleast 0 times.