This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in continue and break

 
Divya Gehlot
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Nik Arora
Ranch Hand
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a alot. I got it
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ketul Shah
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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".
 
suresh koutam
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic