Do While loop does not execute if condition is false?
Show
Details about syntaxwhile (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 loopingThe 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 loopIn this case, the operand or operands of the while statement’s true/false expression are modified in the while statement’s code block. Example 1while (count > 0) { // do some work // ... // change the value of count count = count - 1; }Example 2In 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 validationExample 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 expressionThere 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 operatorsIt is very common when programming to want to add or subtract one from a variable. Therefore, there are two handy shortcut operators.
Prefix and postfix modeYou can place the increment and decrement operators before (prefix mode) or after (postfix mode) the operand.
Effect of prefix and postfix mode in simple expressionsIn 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 decrementIn statements that do more than increment or decrement a variable, the prefix and postfix modes result in very distinct differences.
Stopping looping by use decrement in while true/false expressionIn 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. CountersThe 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 counterThe 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.
Example of counting down using a counterThe 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.
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 loopsAn 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 stopUse 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 loopsWhen 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. While is a pretest loopBecause 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. Letting users control the number of loopsThere are several ways to allow users to control the number of loops. Example 1The user is asked how many test results they want to enter and then the program loops the required number of times. Example 2In 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. Video lecturesMore Repetitive Execution (while loops and do-while loops) - by Jennifer Parham-MocelloThe While Loop (demo) - by Jennifer Parham-MocelloCommon Mistakes with While Loops (demo) - by Jennifer Parham-MocelloWhat happens when the condition is false in doIf the expression is false, the loop terminates and control transfers to the statement following the do-while loop.
Dowhile 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 doWhen condition becomes false, control passed to the first statement that follows body of the loop. While loop will be executed atleast 0 times.
|