Java, C, Python and nested loops

Java has no goto statement, to break or continue multiple-nested loop or switch constructs, Java programmers place labels on loop and switch constructs, and then break out of or continue to the block named by the label. The following example shows how to use java break statement to terminate the labeled loop:

public class BreakLabel
{
    public static void main(String[] args)
    {
        int[][] array = new int[][]{{1,2,3,4},{10,20,30,40}};
        boolean found = false;
        System.out.println("Searching 30 in two dimensional int array");

        Outer:
        for (int intOuter = 0; intOuter < array.length ; intOuter++) {
            Inner:
            for (int intInner = 0; intInner < array[intOuter].length; intInner++) {
                if (array[intOuter][intInner] == 30) {
                    found = true;
                    break Outer;
                }
            }
        }

        if (found == true) {
            System.out.println("30 found in the array");
        } else {
            System.out.println("30 not found in the array");
        }
    }
}

Use of labeled blocks in Java leads to considerable simplification in programming effort and a major reduction in maintenance.

On the other hand, the C continue statement can only continue the immediately enclosing block; to continue or exit outer blocks, programmers have traditionally either used auxiliary Boolean variables whose only purpose is to determine if the outer block is to be continued or exited; alternatively, programmers have misused the goto statement to exit out of nested blocks.

What’s interesting is that Python rejected the labeled break and continue proposal a while ago. And here’s why:

Guido van Rossum wrote:

I’m rejecting it on the basis that code so complicated to require this feature is very rare. While I’m sure there are some (rare) real cases where clarity of the code would suffer from a refactoring that makes it possible to use return, this is offset by two issues:

1. The complexity added to the language, permanently.

2. My expectation that the feature will be abused more than it will be used right, leading to a net decrease in code clarity (measured across all Python code written henceforth). Lazy programmers are everywhere, and before you know it you have an incredible mess on your hands of unintelligible code.

But what’s more interesting is that the idea of adding a goto statement was never mentioned. Common sense perhaps?

About these ads

4 thoughts on “Java, C, Python and nested loops

  1. The decisions lately by the internals group have me a bit worried, im seriously considering another language such as python or ruby. No language is perfect, but seriously, who thought adding GOTO would be a good bullet point on the list of new php features? And Why\Do\Namespaces\Have\To\Look\So\Ugly? Oh right, because it wasn’t designed to support them =\

    Rant.Close() =]

  2. @Federico

    I think php group wants to implement best features for all php developers (like us), but dude, for real, something like rename (@link) strtotime to str_to_time to follow the “php naming convention” is not so hard, still, namespaces link is hilarious :).

  3. I’m with van Rossum there. In 10 years of java programming, I have used a break or continue to a label maybe twice in total.
    If that example was my code, you would probably find the outer loop in a “boolean contains(int x, int[][] xs)” method and instead of a “break”, you would find a “return true” in the inner loop, removing the need for the “found = true” assignment in the inner loop as well.

    Gains: Less variables and labels to track in your mind, a reusable query-method and a more readable main-method:

    int[][] array = new int[][]{{1,2,3,4},{10,20,30,40}};
    if(contains(30, array)){
        System.out.println("30 found...")
    } else{
        System.out.println("30 not found...")
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s