Does 'Elseif' Still Exist

Does 'elseif' still exist?

It's elsif, not elseif.

Slightly confusing when you're new, probably.

Does an else if statement exist?

As per the C11, chapter §6.8.4, selection statements, the available statement blocks are

if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement

and, regarding the else part,

An else is associated with the lexically nearest preceding if that is allowed by the
syntax.

So, there is no else if construct, exists as per standard C.

Obviously, an if (or if...else) block can exist as the statement in else block (nested if...else, we may say).

The choice of indentation , is left to the user.


Note:

Just to add, there is no reason for a separate else if construct to exist, the functionality can be achieved by nesting. See this wiki article. Relevant quote

The often-encountered else if in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent "if then else" statements combined with a particular source code layout. However, this also means that a distinct else-if construct is not really needed in these languages.

Do else if statements exist in C#?

You are correct; there is no such thing as an "else if" statement in C#. It's just an else where the statement of the alternative clause is itself an if statement.

Of course, the IDE treats "else if" as special so that you get the nice formatting you'd expect.

Note that there is an #elif construct in the "preprocessor" syntax.

Note also that C, C++ and ECMAScript - and I am sure many more C-like languages - also have the property that there is no formal "else if" statement. Rather, in each the behaviour falls out of the definition of "else" as coming before a single statement.

Do I need a last `else` clause in an `if...else if` statement?

The ending else is not mandatory as far as JavaScript is concerned. As for whether it is needed, it depends on what you want to achieve.

The trailing else clause will execute when none of the specified conditions is true. If the conditions are collectively exhaustive, then an else clause is entirely superfluous, except possibly to contain an assertion that catches the "impossible" condition. In your case, whether you need an else clause depends on whether you want specific code to run if and only if neither of condition1, condition2, and condition3 are true.

else can be omitted for any if statement, there is nothing special in the last if of an if/else if chain. This is documented in any JavaScript grammar, e.g. in the specification.

Can I use `else if` over `elsif`?

You can use else if and it's safe. However note that this means extra end keywords are needed.

if n == 1
puts "foo"
elsif n == 2
puts "bar"
end

is logically the same as:

if n == 1
puts "foo"
else if n == 2
puts "bar"
end
end

or the equivalent:

if n == 1
puts "foo"
else
if n == 2
puts "bar"
end
end

What are the differences between if, else, and else if?

**IF** you are confused
read the c# spec
**ELSE IF** you are kind of confused
read some books
**ELSE**
everything should be OK.

:)

Why we use if, else if instead of multiple if block if the body is a return statement

if-elseif-else statements stop doing comparisons as soon as it finds one that's true. if-if-if does every comparison. The first is more efficient.

Edit: It's been pointed out in comments that you do a return within each if block. In these cases, or in cases where control will leave the method (exceptions), there is no difference between doing multiple if statements and doing if-elseif-else statements.

However, it's best practice to use if-elseif-else anyhow. Suppose you change your code such that you don't do a return in every if block. Then, to remain efficient, you'd also have to change to an if-elseif-else idiom. Having it be if-elseif-else from the beginning saves you edits in the future, and is clearer to people reading your code (witness the misinterpretation I just gave you by doing a skim-over of your code!).

Conditional statement true in both parts of if-else-if ladder

No, they won't both execute. It goes in order of how you've written them, and logically this makes sense; Even though the second one reads 'else if', you can still think of it as 'else'.

Consider a typical if/else block:

if(true){
// Blah
} else{
// Blah blah
}

If your first statement is true, you don't even bother looking at what needs to be done in the else case, because it is irrelevant. Similarly, if you have 'if/elseif', you won't waste your time looking at succeeding blocks because the first one is true.

A real world example could be assigning grades. You might try something like this:

if(grade > 90){
// Student gets A
} else if(grade > 80){
// Student gets B
} else if(grade > 70){
// Student gets c
}

If the student got a 99%, all of these conditions are true. However, you're not going to assign the student A, B and C.

That's why order is important. If I executed this code, and put the B block before the A block, you would assign that same student with a B instead of an A, because the A block wouldn't be executed.

Are elseif and else if completely synonymous?

From the PHP manual:

In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

Essentially, they will behave the same, but else if is technically equivalent to a nested structure like so:

if (first_condition)
{

}
else
{
if (second_condition)
{

}
}

The manual also notes:

Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

Which means that in the normal control structure form (ie. using braces):

if (first_condition)
{

}
elseif (second_condition)
{

}

either elseif or else if can be used. However, if you use the alternate syntax, you must use elseif:

if (first_condition):
// ...
elseif (second_condition):
// ...
endif;

When to use else if instead of if?

First part:

 if (condition_1)
do this

// previous if has nothing to do with this if & else

if (condition_2)
do that
else
do blablabla

This case if condition_2 is true then do that will be executed, otherwise do blablabla
will be executed..

Now the second part:

if (condition_1)
do this
else if (condition_2)
do that
else
do blablabla

Here, the first true condition will be executing and rest of else if & else will be ignored, all conditions will be checked sequentially till else or a true condition is found.
If no conditions hold then else will be executed. So do blablabla will be executed if those both of two conditions are false

And finally, yes second if can be replaced by:

if (!(condition_1) and condition_2)
do that

This is because second if will be checked (do that will be executed) only if the condition_1 is false.. and condition_2 is true.

Which is equivalent to: (if and only if the condition_1 is false)

if (!(false) and condition_2)
do that

You can replace else ifs by checking whether previous condition was false, this way each else ifs with previous conditions, this is tedious.

Example:

if (a) {
// do task1
} else if (b) {
// do task2
} else if (c) {
// do task3
} else {
// do task4
}

Is equivalent to:

if (a) {
// do task1
}
if (!a and b) {
// do task2
}
if (!a and !b and c) {
// do task3
}
if (!a and !b and !c) {
// here is the else
// do task4
}

When to use else if instead of if?

Generally speaking, we chain if, else if's and an else at the last to ensure execution of only one condition, if one is true rest are ignored and executions goes to the next of very last line ( } ) of last else.



Related Topics



Leave a reply



Submit