How do these practically differ?
// Approach one if (x == 1) DoSomething(); else if (x == 2) DoSomethingElse(); // Approach two if (x == 1) DoSomething(); if (x == 2) DoSomethingElse();
Is the resulting CIL the same?
(The question Use of “if/elseif/else” versus “if/else{if/else}” exists, but it is yet to be answered.)
Summary: By using an ELSE IF structure instead of multiple IF we can avoid “combined conditions” ( x<y && y<z ). Instead we can use a simplified condition (y<z). Furthermore ELSE IF is more efficient because the computer only has to check conditions until it finds a condition that returns the value TRUE.
In an if...else statement, if the code in the parenthesis of the if statement is true, the code inside its brackets is executed. But if the statement inside the parenthesis is false, all the code within the else statement's brackets is executed instead.
Yes, both C and C++ allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.
The nested if is an if statement used within another if statement. When we use if else if then an if statement is used within the else part of another if in this way,'nested if is similar to an if else if statement.
If DoSomething
sets x
to 2, then they will differ.
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