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{}
Millions saw the apple fall, but Newton asked why.
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
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.
~Bill
SCJP
Visit my download page
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.
I agree that the particular solution depends on the task at hand.
Millions saw the apple fall, but Newton asked why.
Consider Paul's rocket mass heater. |