2

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?

M A
  • 71,713
  • 13
  • 134
  • 174
Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
  • Yes, your understanding is correct. Also `++` will make sure that `i` will become = or > `j` at some point. – Nivas Sep 10 '14 at 18:44
  • 1
    @Aeshang That is not correct. The loop will "run" till the condition is being met. – Nivas Sep 10 '14 at 18:45
  • One other thing to note: if you change the initial values so that `i` starts out greater than `j`, this will *still* increment `i` once, and decrement `j` once. – ajb Sep 10 '14 at 19:01

5 Answers5

8

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Would it be appropriate to do `while (list.remove(value));` to remove all occourences of a value from an ArrayList, given that List#Remove specifies in it's javadoc: `return true if this list contained the specified element` – Wasabi Thumbs May 13 '21 at 20:49
8

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.

Community
  • 1
  • 1
Simone Gianni
  • 11,426
  • 40
  • 49
  • Herbert Schildt writes it to be a loop with no body. I don't think he is going to be wrong. – Farhan stands with Palestine Sep 10 '14 at 18:54
  • Your original post left out number 3. StackOverflow renumbered it for you, so the "line numbers" are now wrong. – ajb Sep 10 '14 at 18:56
  • 4
    I dont know who Herbert is but Simone Gianni is explaining how this java code looks like on a low-language level, and is correct. This is what "goes on" during a while statement – Chronicle Sep 10 '14 at 18:56
  • I fixed the line numbers. – ajb Sep 10 '14 at 18:58
  • @ShirgillAnsari in the java code the body is empty, when java compiles it there are instructions in it. All the code before the final condition evaluation (you could call methods, read from a stream, etc..) are executed in each loop. – Simone Gianni Sep 10 '14 at 19:00
  • Added more considerations on "empty" loops, taking into consideration side effects. – Simone Gianni Sep 10 '14 at 19:11
2

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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.

brso05
  • 13,142
  • 2
  • 21
  • 40
0

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.

Simon Baars
  • 1,877
  • 21
  • 38