While loop without a body in Java

14,738

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
14,738
Farhan Shirgill Ansari
Author by

Farhan Shirgill Ansari

A Cheetah that can't sprint is as good as dead Familiar with Google apigee edge platform(API gateways) Software AG ESB Integration Servers Java Plugins Jquery AngularJS Git Presently reading Linux in Action by David Clinton Docker in Action by Jeff Nickoloff and Stephen Kuenzli

Updated on June 14, 2022

Comments

  • Farhan Shirgill Ansari
    Farhan Shirgill Ansari almost 2 years

    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?