Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"else if()" versus multiple "if()"s in C#

Tags:

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.)

like image 236
Jerry Nixon Avatar asked Jan 16 '13 00:01

Jerry Nixon


People also ask

Why use else if instead of multiple if?

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.

What is the difference between IF and ELSE IF in C?

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.

Can you use multiple if statements in C?

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.

What is the difference between if else and nested 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.


1 Answers

If DoSomething sets x to 2, then they will differ.

like image 137
Oliver Charlesworth Avatar answered Oct 05 '22 23:10

Oliver Charlesworth