Labeled loop in Java

undefined label in java
goto label in java
branching and looping in java
labelled statement in java
for loop in java
jumps in loops in java
for loop programs in java
nested for loop java

I having been staring at my screen for a while and I do really need an explanation for labeled loop in this scenario:

package com.misterkourouma.oca8.starter.test;

public class LabeledLoop{

    public static void main(String[] args) {
        int x = 5, j = 0;
        OUTER: for (int i = 0; i < 3;) // -> This line has no curly braces but still compiles
            INNER: do {
                i++;
                x++;
                if (x > 10)
                    break INNER;
                x += 4;
                j++;
            } while (j <= 2);

        System.out.println(x);
    }
}

But this one does not compile :

package com.misterkourouma.oca8.starter.test;

public class LabeledLoop2{
    public static void main(String[] args) {
        int x = 5, j = 0;
        OUTER: for (int i = 0; i < 3;)
            System.out.println("Labeled Loop");
            INNER: do {
                i++;
                x++;
                if (x > 10)
                    break INNER;
                x += 4;
                j++;
            } while (j <= 2);
        System.out.println(x);
    }
}

All the INNER: block are considered (I guess) as a single statement but It does not end with semicolon I wonder Why? I am preparing for OCA 8 that's one of the reason I need to understand these weirds stuffs.

EDIT: My question is on LabeledLoop example why does it compiles Thanks in advance for your help.

As per my understanding you would be getting the compilation error at the below statement:

 i++;

It is because of the fact that you are not creating any block at OUTER: for (int i = 0; i < 3;) statement(by using curly braces{}), thus, by default scope of the OUTER loop remains till the very next statement(in your case at System.out.println("Labeled Loop");). When you increment the variable i after three statements , it would be giving you compilation error, stating i is undeclared.

Understanding the Java Labeled Statement, Following program is using labeled for loops.ExampleLive Demopublic class Tester { public static void main(String args[]) { first: It is a label, and labels in Java can be used with the break and continue key words for additional control over loops. Here it is explained in a rather good way: Thinking in Java, break and continue

Curly Brackets are important in Java, while indents are not.

This code:

public static void main(String[] args) {
    int x = 5, j = 0;
    OUTER: for (int i = 0; i < 3;)
        System.out.println("Labeled Loop");
        INNER: do {
            i++;
            x++;
            if (x > 10)
                break INNER;
            x += 4;
            j++;
        } while (j <= 2);
    System.out.println(x);
}

means:

public static void main(String[] args) {
    int x = 5, j = 0;
    OUTER: for (int i = 0; i < 3;)
        System.out.println("Labeled Loop");
    INNER: do {
        i++;
        x++;
        if (x > 10)
            break INNER;
        x += 4;
        j++;
    } while (j <= 2);
    System.out.println(x);
}

which, of course does not compile.

Java labelled for loop, A labeled continue statement skips the current iteration of an outer loop marked with the given label. The following example program, ContinueWithLabelDemo ,​  first is the label for first outermost for loop and continue first cause the loop to skip print statement if i = 1; second is the label for second outermost for loop and continue second cause the loop to break the loop.

If there are no braces after for loop - it is considered that only the first expression is inside the loop. so the first example is equivalent to

        OUTER: for (int i = 0; i < 3;){
            INNER: do {
                i++;
                x++;
                if (x > 10)
                    break INNER;
                x += 4;
                j++;
            } while (j <= 2);
        }
        System.out.println(x);

The variable i is declared in the for loop. In the first example do-while is inside of for loop so everything is ok. The second example is equivalent to

        OUTER: for (int i = 0; i < 3;){
            System.out.println(x);
        }
        INNER: do {
                i++;
                x++;
                if (x > 10)
                    break INNER;
                x += 4;
                j++;
        } while (j <= 2);

So variable i occurs out of scope and not declared in the while loop

Branching Statements (The Java™ Tutorials > Learning the Java , It is not a keyword it is a label . Usage: label1: for (; ; ) { label2: for (; ; ) { if (​condition1) { // break outer loop break label1; } if (condition2) { // break  Java provides a feature not very frequently used, called the labeled statement. This feature is somewhat reminiscent of assembly language programming in the sense that we can create a labeled block of code and refer to the label when transferring program control to the end or beginning of the labeled statement with the keywords break and continue , respectively.

You have missed the {} braces around the for loop. That is the reason, the code is not compiling. It is not recognising the variable i. As i scope is

OUTER: for (int i = 0; i < 3;) {
                    System.out.println("Labeled Loop") only

     public static void main(String[] args) {
                int x = 5, j = 0;
                OUTER: for (int i = 0; i < 3;) {
                    System.out.println("Labeled Loop");
                    INNER: do {
                        i++;
                        x++;
                        if (x > 10)
                            break INNER;
                        x += 4;
                        j++;
                    } while (j <= 2);
                System.out.println(x);
                }
            }

"loop:" in Java code. What is this, and why does it compile?, In this java tutorial we will see example of break and continue statement in Java and some important points related to breaking the loop using  Java has three types of jumping statements break, continue and return. Labelled break, labelled continue, labelled loops. The Java jumping statements are the control statements which transfer the program execution control to a specific statement.

How to Use Break, Continue, and Label in Loop , 1- Loops in Java; 2- for Loop; 3- for-each loop; 4- while Loop; 5- do-while Loop; 6​- The break statement in loop; 7- The continue statement in a loop; 8- Labelled  They can use labels which are valid java identifiers with a colon. Labeled blocks can only be used with break and continue statements. They must be called within its scope. You can not refer them scope of labeled block. The break statement immediately jumps to the end (and out) of the appropriate compound statement. The continue statement immediately jumps to the next iteration (if any) of the appropriate loop.

Loops in Java, Java labeled blocks are logically similar to to goto statements in C/C++. A label or a block statement, only to compound statements that loop: for, while, and do. Labeled For Loop Java Simple For Loop A simple for loop is the same as C / C++. We can initialize the variable, check condition and increment/decrement value.

Labeled Statements in Java, Unlabeled break statement is used to terminate the loop containing it and can be used with switch, for, while and do-while loops. break in java example. Here is an​  Labeled continue Statement. Till now, we have used the unlabeled continue statement. It is used to terminate the innermost loop and switch statement. However, there is another form of continue statement in Java known as labeled contine. We can use the labeled continue statement to terminate the outermost loop as well.