+ 3

What does it mean to put somthing inside {}

for example int x = 10; while(x > 0){System.out.println("hello");--x;} **output hello x10,** why wouldent it do the same thing if i just int x 10; while(x > 0) System.out.println(x);--x; **output time limit exceeded** ain't they both doing the same thing?

27th Jun 2017, 9:06 AM
D_Stark
D_Stark - avatar
2 Answers
+ 6
You are only allowed 1 statement after a while, for loop or if without the use of brackets. Any more than 1 and the brackets are required to run the next statement(s) inside the while loop. This is why you get the infinite loop in your example. System.out.println(x);--x; <-- 2 statements You could, however accomplish it by doing: while(x > 0) System.out.println(x--);
27th Jun 2017, 9:35 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
thank you
27th Jun 2017, 9:36 AM
D_Stark
D_Stark - avatar