I need to execute code if any of a series of conditional statements, such as if + else if + else if...
, are true:
if(){
...
}else if(){
...
}else if(){
...
}
//run something if ANY of the above conditions are met
I don't want to clutter my code with the same line that needs to be executed after each if
or else if
.
My solution involves:
temp=i;//store a copy
i=-1;//make the change
if(){
...
}else if(){
...
}else if(){
...
}else{
i=temp//restore original value if none was executed
}
This setup guarantees the change will take place regardless and restores the initial state with an else
. However, I am concerned about the readability of this approach.
Are there clearer and more readable alternatives for achieving the same outcome?