Search code examples
javastringbreakgotocontinue

Using 'goto' to move from if to else


I know that there's no goto in java, but I ask on general if there are a method to goto from if to else example :

public void decToAsci(String decimal) {
    String nb = "";
    int number;
    for (int i = 0; i < decimal.length(); i++) {
        Character c = decimal.charAt(i);
        if (!c.equals(' ')) {
            nb += decimal.charAt(i);
            if (i == decimal.length() - 1)
                //here i want to jump to else
                // i can do it like this :
                // System.out.print( (char) Integer.parseInt(nb) + " ");
                // but i repeat code in else here ..i just want to jump to else 
        } else {
            number = Integer.parseInt(nb);
            System.out.print((char) number + " ");
            nb = "";
            number = 0;
        }
    }
}

I try this :

if (!c.equals(' ')) {
    nb += decimal.charAt(i);
    if (i == decimal.length() - 1) continue label;
} else {
    label: the code in else
}

But not worked, not worked with break label also. I understand that continue, break. Are used between son and parent like that? :

label:
for(int i=0 ; i<n ; i++)
    if (cond) break label;

I ask about a method that can jump from if to else (2 sons)

(not a son and a parent but jumping between 2 sons )

I think even with goto on c and c++ 4 example we can't do it


Solution

  • Anything that uses "goto like" behavior is a bad idea. It is widely accepted that goto-like control structures lead to code that is a lot more difficult to read / maintain than if you stick to "structured programming" control structures.

    Here are (IMO) two "right approaches" to solving the problem:

    Approach #1 - rearrange the logic

    public void decToAsci(String decimal) {
        String nb = "";
        for (int i = 0; i < decimal.length(); i++) {
            Character c = decimal.charAt(i);
            if (!c.equals(' ')) {
                nb += decimal.charAt(i);
            }
            if (c.equals(' ') || i == decimal.length() - 1) {
                int number = Integer.parseInt(nb);
                System.out.print((char) number + " ");
                nb = "";
            }
        }
    }
    

    (If the c.equals(' ') was expensive you could use a temporary variable to avoid recomputing it. In this case, it is cheap, and will probably be inlined and further optimized by the JIT compiler.)

    Approach #2 - refactor code into a methid

    public void decToAsci(String decimal) {
        String nb = "";
        for (int i = 0; i < decimal.length(); i++) {
            Character c = decimal.charAt(i);
            if (!c.equals(' ')) {
                nb += decimal.charAt(i);
                if (i == decimal.length() - 1)
                    nb = processAsInt(nb);
            } else {
                nb = processAsInt(nb);
            }
        }
    }
    
    public String processInt(String nb) {
        int number = Integer.parseInt(nb);
        System.out.print((char) number + " ");
        return "";
    }
    

    In this context, the first approach gives a better code. (IMO)