Suppose a loop should execute while x is less than the sum of two integers, a and b. The loop could be written as:while(x < a + b)// loop bodyAlthough this code fragment will run, it is not particularly efficient. In what way is it inefficient, and how could you improve the loop's performance?
What will be an ideal response?
You can improve performance by making sure the loop does not include unnecessary operations or statements. If the while loop in the problem executes 1000 times, then the expression a + b is calculated 1000 times. Instead, if you use the following code, the results are the same, but the arithmetic is performed only once:
int sum = a + b;
while(x < sum)
// loop body
Computer Science & Information Technology
You might also like to view...
The NFTS file system stores information about the location of files, file names, and so forth in the _________________
a. File allocation tables b. Superblock c. Inode d. Master file table
Computer Science & Information Technology
Which of the following protocols would a multilayer switch use to learn the IP address of a directly connected device?
A. ARP B. BOOTP C. DNS D. DHCP
Computer Science & Information Technology