What is the difference between nested and cascaded if-else?
There's not really a difference. The if, else if, else conditional is actually the same as the nested one with one of the {} enclosures removed.
Overview. If else statements are used for decision making by specifying which block of code is to be executed when a certain condition is met. Nested if-else statements are just if-else statements inside other if-else statements to provide better decision making.
Cascaded: if (condition1) { // do one thing } if (condition2) { // do other thing } Here, if condition1 is true, one thing will be done. Again, if condition2 is true, other thing will be done too. Nested: if (condition1) { // do one thing if (condition2) { // do other thing } }
A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
These two are equivalent:
if (condition1) block1
else if (condition2) block2
if (condition1) block1
else
{
if (condition2) block2
}
I presume they also compile to the same assembly, so there should be no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With