When a loop might execute many times, it becomes increasingly important to consider the number of evaluations that take place. How can considering the order of evaluation of short-circuit operators affect the performance of a loop?

What will be an ideal response?


The expressions in each part of an AND or OR expression use short-circuit evaluation; that is, they are evaluated only as much as necessary to determine whether the entire expression is true or false. Suppose that a user can request any number of printed copies of a report from 1 to 15, and you want to validate the user's input before proceeding. If you believe that users are far more likely to enter a value that is too high than to enter a negative value, then you want to start a loop that reprompts the user with the following expression:
while(requestedNum > 15 || requestedNum < 1)?
Because you believe that the first Boolean expression is more likely to be true than the second one, you can eliminate testing the second one on more occasions. The order of the expressions is not very important in a single loop, but if this loop is nested within another loop, then the difference in the number of tests increases.

Computer Science & Information Technology

You might also like to view...

_____ is a system output stream object associated with the screen.

a. cout b. cerr c. cin d. both a &

Computer Science & Information Technology

Briefly describe the difference between a document theme and a style

What will be an ideal response?

Computer Science & Information Technology

Browser software displays Web pages

Indicate whether the statement is true or false

Computer Science & Information Technology

When you publish data to Word from Access, an RTF format document is created.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology