Code Enthusiastic
Code Enthusiastic

Reputation: 2847

how can I come out of if block?

Consider this code sample.

This code just serves to explain my question.

boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        if ( status == false ) {
               System.out.println ( "Enter into second if" );
               status = true;
               if ( status == true ) {
                    System.out.println ( "Enter into third if" );
                    //third if body
               }
               //second if body                             
        }
        //first if body
        System.out.println ( "Before exiting first if" );
    }
}

All I want to do is come out from third if to first if.

We know break, continue are there that can be used in loops. Can we achieve the same for the blocks?

Upvotes: 4

Views: 19634

Answers (6)

Jacob Wong
Jacob Wong

Reputation: 9

Adding the simple combination of while loop and break statement may help.

boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        while(True) {
            if ( status == false ) {
                System.out.println ( "Enter into second if" );
                status = true;
                if ( status == true ) {
                    System.out.println ( "Enter into third if" );
                    break;//The key statement
                    //third if body
                }
                //second if body                             
            }
            break;//Don't forget break while loop
        }          
        //first if body
        System.out.println ( "Before exiting first if" );
    }
}

Or you may use try .. catch ...

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599571

I'd introduce a second status flag to catch that innermost condition:

boolean secondStatus = false;
boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        if ( status == false && secondStatus == false ) {
               System.out.println ( "Enter into second if" );
               status = true;
               if ( status == true ) {
                    System.out.println ( "Enter into third if" );
                    //third if body
                    secondStatus = true;
               } else {
                   //second if body
               }                           
        }
        //first if body
        System.out.println ( "Before exiting first if" );
    }
}

Upvotes: 1

Moritz Petersen
Moritz Petersen

Reputation: 13057

Create a method that encapsulates if #2 and #3. Then return from inside of the #3 if block:

boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        status = perform2ndAnd3rdIf(status);
        System.out.println ( "Before exiting first if" );
    }
}

private boolean perform2ndAnd3rdIf(boolean status) {
    if ( status == false ) {
        System.out.println ( "Enter into second if" );
        status = true;
        if ( status == true ) {
            System.out.println ( "Enter into third if" );
            return status;
            //third if body
        }
        //second if body                             
    }
    return status;
}

Using a method instead of a nested if block improves overall readability and the ability to understand your code. Instead of code comments, you should give that method a meaningful name (of course not perform2ndAnd3rdIf()).

Upvotes: 3

McDowell
McDowell

Reputation: 108939

Well, you can break to any block:

public class Bar {

  static void foo(boolean b) {
    foo: {
      if (b) {
        break foo;
      }
      System.out.println(b);
    }
  }

  public static void main(String[] args) {
    foo(true);
    foo(false);
  }
}

Output:

false

See the Java Language Specification for more.

However, if you tried to deliver this code in one of my projects we would probably have words.

Upvotes: 7

Adam Arold
Adam Arold

Reputation: 30548

You can always do this:

mylabel:
boolean status = false;
for ( int i = 0; i <= 5; i++ ) {
    if ( i == 4 ) { 
        System.out.println ( "Enter into first if" );
        if ( status == false ) {
               System.out.println ( "Enter into second if" );
               status = true;
               if ( status == true ) {
                    System.out.println ( "Enter into third if" );

                    break mylabel;

                    //third if body
               }
               //second if body                             
        }
        //first if body
        System.out.println ( "Before exiting first if" );
    }
}

but I think that you should refactor your code instead.

Upvotes: 2

Renjith
Renjith

Reputation: 3274

Im not sure of your requirement which you are trying to achieve by this.Put one more if condition after the third loop, and if the condition is satisfied only, execute the remaining portion of second loop.You can set this condition inside third if block.

Instead of all this, you can just switch to 'switch' which will be much cleaner.

Upvotes: 1

Related Questions