While loop without a body in Java

The body of the while (or any other of Java's loops) can be empty. This is because a null statement (one that consists only of a semicolon) is syntactically valid in Java. Now consider this :

i = 100     

j = 200   

while(++i < --j); // no body in this loop  

  system.out.println(i);         

  system.out.println(j);      

I noticed that if the value of i is less than j, then the loop repeats itself and when the condition fails to fulfill, then only the 2 below statements are exceuted. Am i correct or is there any other logic behind this?

Upvotes: 2

Views: 19060

Answers (5)

Simon Baars
Simon Baars

Reputation: 2289

I would like to add that you should omit the indentation for the two statements (println statements) below the while loop. You should not see this as a conditional, like an if-statement.

Upvotes: 0

Simone Gianni
Simone Gianni

Reputation: 11662

Answers already given are correct.

However i want to point out that it's not true that the loop has no body. You have to consider it once compiled. It becomes :

  1. Initialize i at 100
  2. Initialize j at 200
  3. Increment i
  4. Decrement j
  5. IF NOT(I < J) jump to 7
  6. Jump to 3
  7. Print and stuff

The loop in this case is from 3 to 6, and has a body (increment i decrement j) and a condition check at point 5.

To make it clearer, I will write another loop, functionally identical, but with a body :

while (true) {
  i++;
  j--;
  if (!(i<j)) break;
}

Now this loop has a body, but probably the compiled version of this loop and the previous one are identical.

However, we could mean by "Empty body loop" a loop that has no side effects. For example, this loop :

for (int i = 0; i < 10000; i++);

This loop is "really" empty : no matter if you execute it or not, it doesn't give or take anything from the rest of the program (if not a delay).

So, we could define it "empty of any meaning" more than "empty of any body".

In this case, the JIT will notice it, and wipe it out.

In this sense, your loop could be :

for (int i = 100, j = 200; i < j; i++, j--);

Here, since "i" and "j" are declared "inside" the loop (doesn't exist outside it's scope), then this loop is meaningless, and will be wiped out.

Maybe this is what Herbert Shildt (that I admit I don't know) means?

Also see here Java: how much time does an empty loop use? about empty loops and JIT.

Upvotes: 8

brso05
brso05

Reputation: 13222

You are correct. The loop will execute the loop body (there is no loop body) until the while condition is met then it will break out of the loop and execute the next 2 statements in sequence.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201419

Yes. The boolean expression will evaluate at each iteration, and when it results in false the loop will terminate. Also, it's System.out.println not system.out.println.

Upvotes: 2

rgettman
rgettman

Reputation: 178253

Yes, it continues to repeat itself without executing any other statements.

It will test the conditions 101 < 199, 102 < 198, and so on, until it tests 150 < 150. Then it breaks out of the loop and executes the println statements.

Output:

150
150

Note that the only reason it's not an infinite loop is that the condition is itself changing the values with the ++ and -- operators.

Upvotes: 8

Related Questions