• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to avoid multiple nested if-else?

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before asking question which subject pattern is 'old question again asked by foolish programmer', i try to explain what i studied:-
Chained if-else statements - how to avoid them?
Avoiding if else loop
google

In program if there are 3 if loops which each contain 5 nested if loops& each has else also then its very complicated to explain/debug/understand afterword also.

eg i try to explain:-


1. 1 If(){
2 1.1if(){
3 1.1.1if(){
4 1.1.1.1if(){
5 1.1.1.1.1if(){
}
else{}
}else{}
}else{}
}else{}
}else{}
1. 2 If(){
2 2.1if(){
3 2.1.1if(){
4 2.1.1.1if(){
5 2.1.1.1.1if(){
}
else{}
}else{}
}else{}
}else{}
}else{}
1. 3 If(){
2 3.1if(){
3 3.1.1if(){
4 3.1.1.1if(){
5 3.1.1.1.1if(){
}
else{}
}else{}
}else{}
}else{}
}else{}



if all 3 if loop are there then very complex & creating more confusion...
What is other way to avoid this?
Is there dependency of this on static variables or classes or functions?

if further example related to this subject then you can see what i write in code.
Actually i want to write/modify that code in proper manner which can be easy to understand/debug.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:Before asking question which subject pattern is 'old question again asked by foolish programmer', i try to explain what i studied:-
Chained if-else statements - how to avoid them?
Avoiding if else loop
google

In program if there are 3 if loops which each contain 5 nested if loops& each has else also then its very complicated to explain/debug/understand afterword also.

eg i try to explain:-


1. 1 If(){
2 1.1if(){
3 1.1.1if(){
4 1.1.1.1if(){
5 1.1.1.1.1if(){
}
else{}
}else{}
}else{}
}else{}
}else{}
1. 2 If(){
2 2.1if(){
3 2.1.1if(){
4 2.1.1.1if(){
5 2.1.1.1.1if(){
}
else{}
}else{}
}else{}
}else{}
}else{}
1. 3 If(){
2 3.1if(){
3 3.1.1if(){
4 3.1.1.1if(){
5 3.1.1.1.1if(){
}
else{}
}else{}
}else{}
}else{}
}else{}



if all 3 if loop are there then very complex & creating more confusion...
What is other way to avoid this?
Is there dependency of this on static variables or classes or functions?

if further example related to this subject then you can see what i write in code.
Actually i want to write/modify that code in proper manner which can be easy to understand/debug.




Minor point ... and if is not a loop, it's just a conditional. one way to do this is as follows:

Another way would be to incorporate switch/case or ternary statements. Not saying that's better, just alternative. It depends on what you are doing. If you can use dynamic Object typing you might be able to skip most of the conditionals. But you might want to read up on that later on.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree. sometimes a switch statement is easier to understand. we can switch on Strings now also(java 7)
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Johnston wrote:
Minor point ... and if is not a loop, it's just a conditional. one way to do this is as follows:

Another way would be to incorporate switch/case or ternary statements. Not saying that's better, just alternative. It depends on what you are doing. If you can use dynamic Object typing you might be able to skip most of the conditionals. But you might want to read up on that later on.


Than conditionals nested that deep, I'd say almost anything would be better. But I agree that the particular solution depends on the task at hand.
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
1. Dennis Deems
2. Randall Twede
3. Bill Johnston
For try to answer....
As per

I agree that the particular solution depends on the task at hand.


My work is as bellow:-
My program is Array of jpanels within jpanel...& that array try to avoid overlapping each other, avoid goes beyond particular x,y axis values.
I have one (if-if-else-else) group which each time calls in following.
I have 18 conditions which should be check by if-else statements.
i have 20 integers (some are static & some are non static). &
1 boolean flag
My if-else structure is as follows...

now for sorting properly this much confusing if -else/else-if conditions what should i do?.....(all conditions are unique)
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My advice in this case would be to divide the body of the giant for loop into several separate methods with clear (and documented) responsibility and corresponding descriptive name. Every of these functions should only have a couple of nested ifs (two are all right, three would be probably over the top, but this depends on your taste and determination). This will flatten the structure out and make the code much easier to understand.
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ugliest code I've ever seen is in this thread. Anyway,

This kind of thing:


can be turned into this kind of thing:



It might take some re-thinking, but if you have nested decisions more than two or three, rarely four, deep then you need to re-think it. IMHO.
I wrote a lot of assembly language and you have equivalents for continue, break, and return, and if, but really no equivalent of else.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why all the empty 'else' statements (e.g. "else { }")? You can omit them.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic