for (int i= 1; i <= 5; i++), int sum = 0;
sum= sum+ (i+2);
Predict the value of sum after the execution
Answers
Answered by
1
Answer:
okay.
int i = 1.
I<=5.
true.
I++ I value increment +1.
i = 6.
now sum = sum +(i +2).
sum = 0+(6+2).
sum = 8.
Answered by
1
Answer:
The value of the sum after the execution will be 25.
Explanation:
The given code block is:
int sum = 0;
for (int i= 1; i <= 5; i++)
sum= sum+ (i+2);
Look at the for loop, it will run five times(from 1 to 5)
Every time when for loop will run, it will add the value of sum and i+2.
- In the first iteration,
- i=1 sum =0
- sum = 0+(1+2) =3
- In the second iteration
- i=2 sum =3
- sum = 3+(2+2) = 7
- In the third iteration
- i=3 sum =7
- sum = 7+(3+2) = 12
- In the fourth iteration
- i=4 sum =12
- sum = 12+(4+2) = 18
- In the last fifth iteration
- i=5 sum = 18
- sum = 18+(5+2) = 25
#SPJ2
Similar questions