Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Nested if()

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having serious trouble understanding how a fix can be put into place without changing the code.
 
Marshal
Posts: 79642
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic