+ 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?
2 odpowiedzi
+ 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--);
+ 2
thank you