question relates to block label and continue/break control flow

S

Shawn

Hi,

I ran into a problem. I need to label a block and use "continue" to
control the program flow in certain conditions.

mainForLoop:
for(int i=1; i < 100; i++)
{
...
if(..)
{
...
continue mainForLoop;
}
else
{
...
}


} //end of for loop, also end of the block mainForLoop

The above code works fine and my program functions correctly. But I hope
to add brackets for block1. The compiler says it is error. My code looks
like:

mainForLoop:
{
for(int i=1; i<100; i++)
{
...
if(..)
{
...
continue mainForLoop; //ERROR: continue cannot be used outside of a
loop. Why?
}
else
{
...
}

} //end of for loop
} //end of block mainForLoop

What is the problem? Ideally, I like the syntax of the second version,
because it is clearer.

Thank you.
 
S

Shawn

To reiterate my problem, I just test a smaller code.
This is OK:
public class Demo2 {
public static void main(String[] args)
{
mainForLoop:
for(int i=1; i<=100;i++)
{
if(i%2 == 0) continue mainForLoop;
System.out.println("i= " + i);
}
}
}


This is NOT OK:

public class Demo2 {
public static void main(String[] args)
{
mainForLoop:
{ //newly added
for(int i=1; i<=100;i++)
{
if(i%2 == 0) continue mainForLoop;
System.out.println("i= " + i);
}
} //newly added
}
}

I cannot understand the error message: "continue cannot be used outside
of a loop". Why an extra pair of brackets makes "outside of a loop"?
 
P

Patricia Shanahan

Shawn wrote:
....
This is NOT OK:

public class Demo2 {
public static void main(String[] args)
{
mainForLoop:
{ //newly added
for(int i=1; i<=100;i++)
{
if(i%2 == 0) continue mainForLoop;
System.out.println("i= " + i);
}
} //newly added
}
}

I cannot understand the error message: "continue cannot be used outside
of a loop". Why an extra pair of brackets makes "outside of a loop"?

"The continue target must be a while, do, or for statement or a
compile-time error occurs."

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.16

The continue target in the working version was a for statement, which is
fine. In the new version the continue target is a block.

The problem is that you have a continue with a target that is not a
loop, not that the continue itself is outside a loop, so the message
seems a bit confusing.

Patricia
 
O

Oliver Wong

Shawn said:
To reiterate my problem, I just test a smaller code.
This is OK:
public class Demo2 {
public static void main(String[] args)
{
mainForLoop:
for(int i=1; i<=100;i++)
{
if(i%2 == 0) continue mainForLoop;
System.out.println("i= " + i);
}
}
}


This is NOT OK:

public class Demo2 {
public static void main(String[] args)
{
mainForLoop:
{ //newly added
for(int i=1; i<=100;i++)
{
if(i%2 == 0) continue mainForLoop;
System.out.println("i= " + i);
}
} //newly added
}
}

I cannot understand the error message: "continue cannot be used outside of
a loop". Why an extra pair of brackets makes "outside of a loop"?

The label should be directly before the loop it's labeling:


public class Demo2 {
public static void main(String[] args)
{
{ //newly added
mainForLoop:
for(int i=1; i<=100;i++)
{
if(i%2 == 0) continue mainForLoop;
System.out.println("i= " + i);
}
} //newly added
}
}

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top