Details around syntax

while (true/false expression)

The true/false expression in the while statement deserve to be anything the evaluates come true or false. As with if statements, 0 is treated as false and any non 0 number is treated together true. Char worths can also be provided as they are treated as integers.

You are watching: The while loop is a pretest loop

Statements adhering to while (true/false expression)

Follow the while(true/false expression) v a single statement or a block the code. The solitary statement or code block is repeatedly executed when the problem is true.

Example

// execute single statementwhile (loopCount) cout

Stopping the looping

*
The if statement will consistently execute the loop statement or password block while the true/false expression is true. To protect against the looping, something requirements to occur to readjust the true/false expression to false.

There are two possible places to change variables provided in the true/false expression, within the password executed during the loop or within the true/false expression itself.

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


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

example 1

while (count > 0) // execute some job-related // ... // change the worth of counting count = counting - 1; instance 2 In this example, the user inputs a brand-new value for testScore during every loop. The if statement will loop until the user input a correct worth within 0 and 100.

int testScore;cout > testScore;while (testScore 100) // testScore is no valid cout > testScore; Note around input validation instance 2 illustrates writing a regimen with intake validation. A fully developed and also complete regime should incorporate input validation. Never assume that inputs are constantly correct.


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


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

Increment and decrement operators

It is really common when programming to want to add or subtract one indigenous a variable. Therefore, there space two handy shortcut operators.

Operator Name activity Pronunciation
++ increment operator add one plus plus
-- decrement operator minus one minus minus
The increment and also decrement operators space unary operator in that they usage a single operand. The increment and also decrement operators occupational on numeric values. The operand must be a non-constant lvalue such together a variable. Girlfriend can’t apply ++ and also -- to literals. Prefix and also postfix mode

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

Expression form Pronunciation
someVariable++ postfix someVariable to add plus
++someVariable prefix plus plus someVariable
someVariable-- postfix someVariable minus minus
--someVariable prefix minus minus someVariable
impact of prefix and also postfix mode in an easy expressions

In basic 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 also postfix mode in statements the do much more than increment or decrement In statements that do much more than increment or decrement a variable, the prefix and postfix modes result in really distinct differences.

If you use prefix mode, the change 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 ~ additiony = 10 + x++;
At the end of this code, x = ? and y = ?
x = 6y = 15
x = 5;// prefix, x incremented before additiony = 10 + ++x;
At the end of this code, x = ? and y = ?
x = 6y = 16
In the complying with example, we want the if statement to loop five times and also stop. By utilizing numberOfInputs-- in the if true/false expression, the if statement counts under from 4 to 0. When 0 is reached, the if expression is same to while(0) so the looping stops.

*

Counters

The above example illustrates what is dubbed a counter. A counter is a variable that is incremented or decremented each time the routine executes the loop. The respond to variable numberOfInputs was decremented every time the regime looped through the when statement.

The one thing that deserve to sometimes trip world up on respond to variables is the they don’t use the suitable relational operators for beginning or finishing counts at 0 versus 1.

It might seem strange to count starting from 0 or ending at 0 yet as girlfriend will watch in later chapters, the first element in lists of objects or worths such as in arrays is in ~ index worth 0 so C++ programmers become very used to counting native 0 or ending at 0.

instance of count up utilizing a respond to

The table below illustrates the appropriate relational operator to use as soon as counting increase from 0 matches 1.

The table provides a counter variable named countVar.

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

countVar starting at appropriate relational operator Relational expression because that looping x times worths of countVar throughout looping
0 Example of counting down making use of a counter

The table listed below illustrates the ideal relational operator to use as soon as counting down to 0 versus 1.

As in the over table, the table offers a counter variable named countVar.

When countVar is decremented by one every time the loop executes and you desire to loop x times, usage the adhering to rules.

beginning value the countVar Count ending at appropriate relational operator Relational expression because that looping x times values of countVar throughout looping
x 1 > 0, 1, 2 ... , x-1 x, x-1, ..., 1
x-1 0 >= while(countVar >= 0) x-1, x-2, ..., 0

Note the you deserve to come increase some other relational expression to add to this tables. The main suggest here is to be mindful of the affect of starting or avoiding at 0 versus 1 and also that you need to be cautious in creating the relational expression so friend don’t loop one too plenty of or one too couple of times.

Here is quick instance of an incrementing counter.

int numberOfInputs = 5;int counting = 0;// use

Infinite loops

*
An infinite loop is one that starts and also never stops. This happens when the true/false expression never ever returns a false value. When there can be some instances where you might want this to happen, most often an unlimited loop is the result of a an insect in your program’s logic.

helpful hint if your program will not stop


Use her keyboard and press CTRL and then c therefore you room holding CTRL and c down at the very same time (called Control-C).

Do no let semicolons cause endless or limitless loops

When you very first start working v while statements, you might accidentally ar a semicolon ~ the “while(true/false expression)” part of the statement such as shown below. The semicolon outcomes in a null statement, a statement the does nothing. In the example below, because of the null statement, the loopCount is never adjusted so the looping never ever stops.

*


While is a pretest loop

Because the while’s true/false expression is evaluated prior to the looping starts, the is a pretest loop. V pretest loops such as the if loop, the program might never execute the loop statements. If the initial review of the while statement’s true/false expression is false, the regime skips every statements the the loop and continues execution after the when statement.

*

Letting users regulate the variety of loops

There space several means to allow users to regulate the variety of loops.

Example 1


The user is request how numerous test results they desire to enter and also then the routine loops the required variety of times.

See more: Why Is Sulfur A Metal Nonmetal Or Metalloid Or Nonmetal? The Parts Of The Periodic Table

*


Example 2


In this example, the user is asked come guess the digit the program has chosen. The program loops until they guess: v correctly however the user can stop the looping in ~ any allude by beginning a non-digit value.

*


Video lectures

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