aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes if() condition exit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "if() condition exit" Watch "if() condition exit" New topic
Author

if() condition exit

Vaibhav Goel
Ranch Hand

Joined: Jul 05, 2007
Posts: 40
Hello there,

We know that break keyword is used to come outside from a while condition. After break, loop terminates and control goes out of loop.
Same thing I want to do with if condition. In my if condition there are several other if contions with while loops as well.
At any certain point I want to come outside the main if block.
Please tell me how can I do so because we cannot use break inside if condition.
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
I think you can do it by using a Conditional break statement.
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
Sorry it is not conditional break statement. it is a named break statement.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
You can do it with a labeled break statement:

Prints: "A"
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
For example see the foloowing small code:
class BreakIF
{
public static void main(String[] args)
{
int i = 10;
FirstBreak:
if(i == 10){
int j = 2;
System.out.println("inside true part");
if(j == 2){
break FirstBreak;
}
while(j == 2){
System.out.println("inside while loop: " + j);

}
}else{
System.out.println("inside else statement");
}

}
}

While loop is an infinite loop, but while loop does not get executed because of early break of if condition. I think this will help you.
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
i think there is one more solution
as an if method can be cared off when it's condition is false ..
look at code

if(SOP("hello")==0)
{
SOP("hello");
}
else
{
SOP("world");
}
//prints "hello world" get it..
so by using proper instance variable we can have if() loop terminated at right time..
(if not correct please let me know)
deepesh mathur
Ranch Hand

Joined: Aug 13, 2007
Posts: 39
i think there is one more solution
as an if method can be cared off when it's condition is false ..
look at code

if(SOP("hello")==0)
{
SOP("hello");
}
else
{
SOP("world");
}
//prints "hello world" get it..

so by using proper instance variable we can have if() loop terminated at right time..
(if not correct please let me know)
 
Don't get me started about those stupid light bulbs.
 
subject: if() condition exit