• 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

doubt in continue and break

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1) Compile time error, System.out has no print method
2) No output at runtime
3) Output of 0
4) Compile error, break cannot occur after its target label

I expected the output to be 01 but when I run the above program got the ouput 0 . Can anybody explain me ?
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi divya,
When i is 0 it comes in the loop and checks for both the condition what you have written since both condition returns false it goes to next line and prints 0. Now i is 1 so the second condition satisfies that is if(i>0), in the if block you have written a continue statement now control goes back to the for loop. i value gets incremented now its value is 2 so the condition fails that is i<2 and control comes out of the for loop. So only 0 gets printed.

Regards
Nik
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(har==ham){ //never gets here because they are not equal


if(i > 0){
continue collar; }
System.out.print(i);

first time condition is false . so it will print 0. In second iteration i>0 condition will true and so it will go continue statement and loop will move to next iteration and then for loop condition will never be true for i after that so no SOP will be printed after first time
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a alot. I got it
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

somebody interested in a small variation of the program?


1) Compile time error, System.out has no print method
2) No output at runtime
3) Output of 0
4) Compile error, break cannot occur after its target label
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:

public class HarHam{
public static void main(String argv[]){
HarHam hh = new HarHam();
hh.go();
}
public void go(){
String har = "har";
String ham = "har";
collar:
for(int i=0; i < 2 ; i ++){
if(har==ham){
break collar;
}
if(i > 0){
continue collar;
}

System.out.print(i);
}
}

}

--------------------------------------
The answer for this is "No Output at runtime".
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep
no output at runtime...
the first condition is met and the break takes it out of the for loop..


Suresh Koutam
 
reply
    Bookmark Topic Watch Topic
  • New Topic