what will display the following program item and how many times will be executed the loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what will display the following program item and how many times will be executed the loop

int x = 1; int z; for(z = 6; z >= 2; z--) x = x + z; System.out.println("x = " + x); System.out.println("z = " + z);

11th Mar 2018, 9:50 PM
Solomiya Chukivska-Marichko
Solomiya Chukivska-Marichko - avatar
2 Answers
+ 1
The loop will run the body (x = x + z;) 5 times (z=6, 5, 4, 3, 2). But the only output will be the final values of x and z. x = 21 z = 1 As it is after the body of the loop. For clarification, the code you have is the same as: int x = 1; int z; for(z = 6; z >= 2; z--) { x = x + z; } System.out.println("x = " + x); System.out.println("z = " + z);
11th Mar 2018, 10:44 PM
ChaoticDawg
ChaoticDawg - avatar
0
Could you, please, explain how will it be if the task is like that... int x = 7; int z; for(z = 1; z <= 6; z++) x =  x+z; x = x * 10; System.out.println("x = " + x); System.out.println("z = " + z);
12th Mar 2018, 5:15 AM
Solomiya Chukivska-Marichko
Solomiya Chukivska-Marichko - avatar