Discuss a situation in which it would be more appropriate to use a do…while statement than a while statement. Explain why.

What will be an ideal response?

If you want some statement or set of statements to execute at least once, then repeat
based on a condition, a do…while is more appropriate than a while (or a for). A do…while statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once. A while tests the loop-con- tinuation condition before executing the loop’s body, so the program would need to include the statement(s) required to execute at least once both before the loop and in the body of the loop. Using a do…while avoids this duplication of code. Suppose a program needs to obtain an integer value from the user, and the integer value entered must be positive for the program to continue. In this case, a do…while’s body could contain the statements required to obtain the user input, and the loop-continuation condition could determine whether the value entered is less than 0. If so, the loop would repeat and prompt the user for input again. This would continue until the user entered a value greater than or equal to zero. Once this criterion was met, the loop- continuation condition would become false, and the loop would terminate, allowing the program to continue past the loop. This process is often called validating input.

Computer Science & Information Technology

You might also like to view...

When using a box layout, how is the orientation – horizontal or vertical box – specified?

What will be an ideal response?

Computer Science & Information Technology

Match each kind of chart with the best example.

A. Each bar represents annual sales for a different product over 12 months. B. Each point represents monthly sales over 12 months. C. Each wedge represents total quarterly sales for a company. D. Each section represents monthly sales by representative, stacked to show cumulative total sales.

Computer Science & Information Technology