hrishikeshp19
hrishikeshp19

Reputation: 9028

java for loop with empty statements

I have seen following pattern in some java code recently. Can somebody explain it to me? What does this for loop mean?

for (;;) {
    try {
        // do something     
    } catch (Exception e) {
        if (e instanceof SomeException) {
            break;
        }
    }    
}

Upvotes: 0

Views: 2350

Answers (5)

JackDev
JackDev

Reputation: 11173

If means the it will continuously loop and perform the actions stated in '//do something' until an instance of 'SomeException' is thrown, in which case the loop will terminate.

They are commonly used in network socket programming, when an application is listening in on a port, and terminates if there are errors.

Explaination

It is a infinite loop, or similar to a 'while(true)' loop statement.

'for' loops are defined as

for([variable declaration]; [ending condition]; [action to do at the end of each loop]){...}

As you can see, with a 'for(;;)' No ending condition is specified and it loops on forever. A 'break' is a must in such a loop to prevent the infinite loop when certain conditions are met.

Personally I don't like using them too much unless absolutely necessary. While there is no significant performance disadvantages (see why does an infinite loop of the unintended kind increase the CPU use?), one has to be very careful when changing the code inside the infinite for loop, so it does exit eventually with a 'break'.

Upvotes: 0

Barranka
Barranka

Reputation: 21047

This is equivalent to something like this:

 while(true) {
     try {
         // Do something
     } catch(Exception e) {
         if(someCondition)
             break;
     }
 }

The loop will run until it is explicitly broken by the exception handler.

Notice that this (and your for(;;)) will be running forever if someCondition is never set to true.

If you insist to do this endless loop, I would modify it to something like:

boolean terminateLoop = false;
while(true) { // for(;;) {
    try {
        // Do something
        if(someTerminationCondition)
            terminateLoop = true;
    } catch(Exception e) {
        if(someCondition)
            break;
    } finally {
        if(terminateLoop)
            break;
    }
}

Note. I wouldn't use this on any real world algorithm... it is just a "theoretical" solution to avoid an endless loop.

The for loop is meant to do things based on specific increments (or decrements) of a variable... I like to call it a "deterministic" loop (I'm not quite sure if it is an appropriate name). If whatever you need to do doesn't gives you a way to use specific increments (or decrements) of a variable as a break condition, I think you should consider using while or do... while loops (which I like to call "non-deterministic" loops).

Upvotes: 0

user2864740
user2864740

Reputation: 61905

tldr; for(;;) {..} is semantically equivalent to while(true) {..}.


Without an "end condition" the for loops forever .. or until a break or return or propagated exception or JVM termination. (With this type of loop, which is equivalent to while(true) {..}, the other parts of the for are also omitted as they are not used.)

In this case there is a break that is invoked if and only if a certain type of exception was caught. Otherwise the code will keep looping where it presumable retries a particular operation again and again while ignoring some types of exceptions. (This might be a case of a "clever programmer" using exceptions for flow control..)


Annotated:

// loop "forever" ..
for (;;) {
    try {
        // .. and perform some action ..
    } catch (Exception e) {
        if (e instanceof SomeException) {
            // .. but terminate the loop on a certain exception ..
            break;
        }
        // .. or otherwise loop again (even on an exception)
    }    
}

Upvotes: 0

Publius
Publius

Reputation: 1224

Given no "break" statement, that loop will loop indefinitely. A "break" statement leaves a loop, regardless of what the exit condition is.

The code block you have there will keep doing the thing in the "Try" block until a SomeException is thrown. Once a SomeException is thrown, the loop will exit. Checking to see if a SomeException is thrown can't be done within the declaration of the loop, so the loop is told to loop forever. Then the condition under which the loop ends is specified within the body of the loop.

Upvotes: 0

iforce2d
iforce2d

Reputation: 8272

This will make an endless loop, so you need a break to get out of it.

Upvotes: 3

Related Questions