Do While loop does not execute if condition is false?

Syntax

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 100] // testScore is not valid { cout > 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.

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 >= 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

Chủ Đề