0

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

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);

24th Jul 2019, 11:46 AM
Сергій Дацюк
Сергій Дацюк - avatar
3 Réponses
+ 12
code is correct syntax wise, what problem you are facing in it ? if its about output, then x will be (7 + (1+2+3+4+5+6))*10 & z will be 7, as only 1 statement is consider to in loop block if no { } are made after loop.
24th Jul 2019, 1:04 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
loop will execute 6 times, and only 1 statement will get executed multiple times, that statement is x=x+z; loop will end when z becomes 7, so final value of z will be 7.
24th Jul 2019, 1:11 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
thank you!
24th Jul 2019, 1:10 PM
Сергій Дацюк
Сергій Дацюк - avatar