Ask Question
3 November, 01:03

In the first loop above, the println statement comes before the value of count is incremented. What would happen if you reversed the order of these statements so that count was incremented before its value was printed

+4
Answers (1)
  1. 3 November, 01:06
    0
    Here is the block of code for this question:

    final int LIMIT = 100; / / setup

    int count = 1;

    while (count < = LIMIT) / / condition

    { / / body

    System. out. println (count); / / - - perform task

    count = count + 1; / / - - update condition }

    Explanation:

    First i will explain the code line by line before answering the question statement.

    The first statement in the code has a variable LIMIT which is assigned the value of 100. The keyword final is used with this variable to make its value constant which means this value cannot be changed.

    Next the int type variable count is declared and initialized by value 1.

    The while loop has the condition which checks if the value of count variable is less than or equal to the value of LIMIT variable (100). This loop will keep iterating until the value of count becomes more than the value of LIMIT which is 100.

    As the current value of count is 1 so yes it is less than LIMIT value.

    So the program control enters the body of the loop which in which the value of count is printed and then the value of count is incremented by 1.

    So the new value of count becomes 2. Now in the second iteration this new value of count will be checked against LIMIT again and then this value of count will be printed again.

    The iterations continue until the value of count exceeds the value of LIMIT. This program basically prints the numbers from 1 to 100.

    Now lets reverse the statements to check what happens.

    final int LIMIT = 100; / / setup

    int count = 1;

    while (count < = LIMIT) / / condition

    { / / body

    count = count + 1; / / - - update condition

    System. out. println (count); / / - - perform task }

    The output is different as the output displayed before reversing these statements.

    The first code fragment displayed the numbers from 1 to 100 in output.

    This code fragment will display the numbers from 2 to 101.

    Lets check how.

    After the while loop condition is checked, the program controls enters the body of the loop. Here the value of count is incremented by 1. The current value of count is 1 so when its incremented by 1, the value of count becomes 2. After this, the new value of count is displayed by this statement: System. out. println (count);

    Then in the second iteration the value of count becomes 3 and then this value is displayed.

    The last value of count which is checked in the while loop condition is 100. When the statements in the body of the loop are executed this value 100 is is first incremented by 1 and becomes 101 and then displayed in the output. Then the loop end when the value of count becomes 101.

    It means that in the first block of code, each value of count was first displayed before it is incremented but in the second code fragment after reversing these two statements, the value of count variable is first incremented and then this value is displayed with each iteration of the while loop.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “In the first loop above, the println statement comes before the value of count is incremented. What would happen if you reversed the order ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers