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.
You might also like to view...
Answer the following statements true (T) or false (F)
1) If an exception handler is to catch a variety of exceptions, use a comma- separated list of catch arguments in the catch handler. 2) Exception handlers typically access objects in their try blocks to determine the causes of the exceptions. 3) Variables local to a try block are usable in the corresponding catch and finally blocks. 4) A NullReferenceException is thrown when there's an attempt to use a reference that points to nothing. 5) There must be a catch clause for every expected exception type.
To calculate the number of montly payments required to pay off a loan or meet an investment goal, use the _______ function.
A. PMT B. FV C. PPMT D. NPER
Which of the following keyboard shortcuts is used to paste the contents of the Clipboard?
A. Ctrl+P B. Ctrl+C C. Ctrl+V D. Ctrl+X
_____ is an example of an addressing mode found in processors.
Fill in the blank(s) with the appropriate word(s).