/* this program checks how labeled continue works */
public class Check
{
public static void main(
String[] args)
{
boolean isPrime = true;
for ( int i=1 ; i <= 50 ; i++ )
{
for ( int j = 2 ; j <= Math.sqrt(i) ; j++ )
{
if ( i % j == 0 ) // divisible
{
isPrime = false;
continue PrintStmt;
}
}
// I want to skip the following 2 statements
System.out.println( " Skip statement 1 ! ");
System.out.println( " Skip statement 2 ! ");
// giving a label
PrintStmt :
// i want to print the following
System.out.println( " Oughta print this ! " );
System.out.println( " Oughta print this too !! " );
if ( isPrime = true )
{
System.out.println( " The number : " + i );
}
}
}
}
The above program gives the error :
Undefined label PrintStmt
Is there any construct similar to a goto label ? Does
java have goto?
Also, i have to give the label for a loop outside the loop.
Especially in thecase of the for loop as in the above example,
i wonder why we should give it outside the loop and not as the first line. Logically thinking, i feel that giving it outside the loop makes you think it starts the whole loop once again including the initialization, which of course is not the case.