JavaProphet
JavaProphet

Reputation: 991

Escape if statement/code block?

Say I have an if block, or any code block for that matter. Is there any good way to escape the rest of the block, like a continue for a loop? I understand goto is defunct in Java.

Example code:

if (sid > -1) {
    if (!godDamnDefunctGoto(session, sid)) {
        sid = -1;
    }else {
        vsid = true;
    }
}

When I should be able to run my checks here, without calling a method. The method godDamnDefunctGoto run various checks on my sid & session variable. I don't want to repeat code over and over from returns in the code flow of that method by placing it in this one.

The goal here is to run some code after after that if statement, but not yet return until this has run. I'd have to run it several times if I were to inline the Java method. Perhaps Java should implement the inline keyword, similar to C?

Upvotes: 1

Views: 2044

Answers (3)

morgano
morgano

Reputation: 17422

Just use the labeled break statement (yes, break DOES work without being inside the while loop) it is little known in java because it is not a good programming practice (it breaks the Structured program theorem, as it does the goto statement)

If you don't believe me, just compile and run this code:

public class Test {

    public static void main(String[] args) {

        boolean myCondition = false;

        MY_BLOCK: {
            System.out.println("1");
            System.out.println("2");
            System.out.println("3");
            System.out.println("4");

            if(!myCondition) break MY_BLOCK;

            System.out.println("5");
            System.out.println("6");
            System.out.println("7");
        }
        System.out.println("END");
    }
}

Upvotes: 0

mikeb
mikeb

Reputation: 11287

You should write everything inside the if statement as a function, and return when you want to break.

Let's say you the following code, and if a > 23 you want to "break"

a = getUserInput();

if (a == 1) {

a = a * 23;
// some other stuff
  if(a > 23){ // you want to "break" if this is true
    System.out.println("break!!");
  }
// Do other stuff

System.out.println(a);

You can put the if code in a function:

public void processA(Integer a){
  a = a * 23;
  // some other stuff
    if(a > 23){ // you want to "break" if this is true
      return;
    }
  // Do other stuff
}

Then, in your code:

if (a == 1) {
  processA(a);
}

System.out.println(a);

This is how you can do it with a method, there's really no way to break out of an if loop that I can think of otherwise.

If you want to pass several variables to the function, instead of making it take an Integer, make it take a CustomObject, or a "bean" as it's known (here is a tutorial):

public class CustomObject{ int intOne; int intTwo; int intThree; // Add getters and setters }

Upvotes: 1

user4910279
user4910279

Reputation:

If you want to do like this

    L1: if (C1) {
        P1();
        if (C2)
            break L1;
        P2();
    }

You can write like this

    while (C1) {
        P1();
        if (C2)
            break;
        P2();
        break;
    }

Upvotes: 1

Related Questions