1

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.

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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); } } 

Comments

0

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.