Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between nested and cascaded if-else

What is the difference between nested and cascaded if-else?

like image 324
steven Avatar asked Mar 28 '10 21:03

steven


People also ask

Is there any difference between a nested IF and cascading if-else statements?

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.

What is the difference between if-else and nested if-else?

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.

What is cascaded if-else statement?

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 } }

What is nested if-else?

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.


1 Answers

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.

like image 61
abc Avatar answered Sep 21 '22 03:09

abc