Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested if()

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you CAN'T change the existing code AT ALL....The only way I see to do it is something like:


 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having serious trouble understanding how a fix can be put into place without changing the code.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.
Is there any other solution other than calling the method from two different places?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic