• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Labeled continue

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/* 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Label should appear before the continue statement.
Hence, you should put the label above the for loop.
Binod.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use of the goto/label machanism is very poor programming as far is style is concerned. But that is not my concern.
Anyway, there is no goto in Java, thank god.
Anyway, the labeled break/continue works a little differently that you have coded.
Without a label 'continue' goes to the next iteration. Break, passes control to any finally clauses and then escapes from loop.
With a label, control may pass to another controlling structure.
Notice how control is passed to an outer loop or another controlling structure, very important.
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guess i have cleared up myself a bit :
( Thanks for the responses... )
A labeled continue allows control to be taken to the next iteration of the looop which is labelled... it need not be the immediate loop ( as is the case with simple continue...)
A labeled break allows to break out of the enclosing labeled loop which may not be the immediately close loop.
Shree
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the break explaination that you got shree is still confusing to me. The break statement breaks out of the loop completely rather than just breaking out of the inner loop.
Would like to know if anybody else too has any complete definition of the keyword 'break' in java.
Thanks
Nasir
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nasir,
The break statement not followed by a label name just breaks out of the inner loop, in other words leaves the inner loop and starts executing the statement following the loop. Where as using the labelled break statement it is possible to break out of the any loop including the outermost loop in which loop structure the break statement exists.
Hope I made myself clear!
Sateesh Donti
------------------
Sateesh Donti
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic