Computer Applications

Write the values that will be stored in variables num and sum after execution of the following code.

int sum = 0, num = -2;
do
{
    sum = sum + num; 
    num++;
} while (num < 1);

Java Iterative Stmts

4 Likes

Answer

After the execution of the given code, num = 1 and sum = -3

Explanation

IterationsumnumRemarks
 0-2Initial values
1-2-1sum = 0 + (-2) = -2
2-30sum = -2 + (-1) = -3
3-31sum = -3 + 0 = -3
Loop terminates

Answered By

2 Likes


Related Questions