File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Nested if() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested if() " Watch "Nested if() " New topic
Author

Nested if()

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
HI all, I have a strange requirement:

My requirement is:
if (condition3) is false, then go to 'else' part given in the above code sample. Above code sample is the existing structure. condition3 is the new enhancement. I can't change the existing code.

Is it possible to achieve this? If not, is there any other way? please suggest.

With Regards,
Anu
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

if you CAN'T change the existing code AT ALL....The only way I see to do it is something like:




There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10246
    
    8

I am having serious trouble understanding how a fix can be put into place without changing the code.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Please confirm there are no } and { which you have hidden behind the ......... because an else refers to the if whose block is completed by the } immediately preceding the else. If Maneesh and Fred don't think it can be done, I know there is no point somebody else trying
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Campbell Ritchie wrote:Please confirm there are no } and { which you have hidden behind the ......... because an else refers to the if whose block is completed by the } immediately preceding the else. If Maneesh and Fred don't think it can be done, I know there is no point somebody else trying



Thanks all for your reply. My problem is :
1) Is it possible to go to outer Most Else if inner most if() condition is false !
2) If I can change the above code, how can i fix the issue ?

Please help.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you want to call the same code from two different places in your if-then-else tree, then put that code into a method and call that method in the two places.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thanks Paul.
Is there any other solution other than calling the method from two different places?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

Java does not have a way to break out of an inner-nested if to the outer else. that really violates the idea of what a if-statement does. You are basically saying


The correct way to do it is to either

a) extract the common code to a method and call it from the two places
b) re-write your logic to something like



Note: I have not verified the above is the correct logic...I don't have time to make out a truth table.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Anu satya wrote:Is there any other solution other than calling the method from two different places?


You could possibly reorganize the if's and else's so that the code only needed to be in one place. However since you don't have a real example of this, just made-up things, anything is possible but nothing can be done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested if()