Question: Consider carefully the program fragment below: int sum = 0, idx = 0; while (idx < 5) { } sum = sum + idx;
Consider carefully the program fragment below: int sum = 0, idx = 0; while (idx < 5) { } sum = sum + idx; idx++; The above loop does at least one unnecessary pass through the body. How can you improve it while not changing the result (the value of sum when the loop ends)? O Initialize the variable sum to 1 rather than 0 O Initialize the variable idx to 1 rather than 0 O Change the while loop condition from idx < 5 to idx < 4 O Change the while loop condition from idx < 5 to idx
Step by Step Solution
There are 3 Steps involved in it
The detailed answer for the above question is provided below The program fragment shown in the image ... View full answer
Get step-by-step solutions from verified subject matter experts
