Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you eplain this code

 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found tht error is with Continue statement...can you explain this. Continue should not be used outside the loop. How can I change this code runnable

public class Example1 {


public static void main(String[] args) {

int i=0;
label:
if(i<2){
System.out.println("I is :" +i);
i++;
continue label;
}
}
}

Thanks...
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A label provides a statement with an identifier that lets you refer to it elsewhere in your program. For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..but in this case ...continue is placed inside a loop...but it shows an eroor tht continue is outside the loop
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"If" statement is not a loop statement.
Label Statement
[ May 28, 2006: Message edited by: wise owen ]
 
Sreeram Desigan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Supriya, plz send the full code so that v can help u.
Contiue can be used only inside loops and if is not a loop. Further the label used for continue should be a loop label.
 
jerry sharma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Supriya !
u can try in this way

public class Example1 {
public static void main(String[] args) {
int i=0;
label:
for (i=0;i<5;i++){
System.out.println("I is :" +i);
i++;
if(i<2)
continue label;
}
}
}
 
vathsala nagaraju
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can put a loop around it

public class Example {


public static void main(String[] args) {

int i=0;
label:
while(true)
{
if(i<2){
System.out.println("I is :" +i);
i++;
continue label;
}
}
}
}
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explaining the code...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic