Programming in C++ Do-While Statement Is a looping control structure in which the loop condition is tested at the end(bottom) of the loop SYNTAX d Statement 3 while( Expression ) Loop body statement can be a single statement or a block Note that the Do-While statement ends with a semicolon
11 Do-While Statement Is a looping control structure in which the loop condition is tested at the end(bottom) of the loop. SYNTAX do { Statement } while ( Expression ) ; Loop body statement can be a single statement or a block. Note that the Do-While statement ends with a semicolon
Programming in C++ Function Using Do-While void GetYesOrNo(/out */char& response) l/Inputs a character from the user ∥ Postcondition: response has been input & response = yor 'n do cin > response; //skips leading whitespace if (( response ly')&&( response In') cout≤≤“ Please type y or n:"; 3 while(( response ='y)&&( response =n));
12 void GetYesOrNo ( /* out */ char& response ) // Inputs a character from the user // Postcondition: response has been input // && response == ‘y’ or ‘n’ { do { cin >> response ; // skips leading whilespace if ( ( response != ‘y’ ) && ( response != ‘n’ ) ) cout << “Please type y or n : ”; } while ( ( response != ‘y’ ) && ( response != ‘n’ ) ) ; } Function Using Do-While
Programming in C++ Do-While Loop vs. While Loop POST-TEST loop ☆ PRE-TEST loop (exit-condition (entry-condition) ☆ The looping condition☆ The looping condition is tested after is tested before executing the loop executing the loop body body ☆ Loop body is always Loop body may not executed at least be executed at all once 13
13 Do-While Loop vs. While Loop ❖POST-TEST loop (exit-condition) ❖The looping condition is tested after executing the loop body. ❖Loop body is always executed at least once. ❖PRE-TEST loop (entry-condition) ❖The looping condition is tested before executing the loop body. ❖Loop body may not be executed at all
Programming in C++ Example While solution Do-While solution sum=0. sum=0 counter=1 counter=1 while(counter<=n) do sum=sum+counter sum=sum+counter. counter++ counter++ y while(counter<=n); 14
14 Example ➢ While Solution sum=0; counter=1; while(counter<=n) { sum=sum+counter; counter++; } ➢ Do-While Solution sum=0; counter=1; do { sum=sum+counter; counter++; } while(counter<=n);
Programming in C++ Example(Cont) >If n is a positive number, both of these versions are equivalent. > But if n is 0 or negative, the two loops give different results In the While version, the final value of sum is 0 because the loop body is never entered aIn the Do-While version. the final value of sum is 1 because the body executes once and then the loop test is made 15
15 ➢If n is a positive number, both of these versions are equivalent. ➢But if n is 0 or negative, the two loops give different results. ▪In the While version, the final value of sum is 0 because the loop body is never entered. ▪In the Do-While version, the final value of sum is 1 because the body executes once and then the loop test is made. Example (Cont.)