Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use 'if…else if' and when to use

1) I know how if…else if statements work, but in the next example both methods are identical as far as the resulting value is concerned. So does it matter which of the two methods I use or should I always go for one that is semantically closest to what the code is trying to do ( here I’m guessing that semantically the two methods are quite different )? So which method would you use and why?

protected string GetNumberDescription(int value)
{
    if (value >= veryBigNumber)
        return veryBigNumberDescription;
    else if (value >= bigNumber)
        return bigNumberDescription;
    else if (value >= smallNumber)
        return smallNumberDescription;
    else
        return "";
}

protected string GetNumberDescription(int value)
{
    if (value >= veryBigNumber)
        return veryBigNumberDescription;
    if (value >= bigNumber)
        return bigNumberDescription;
    if (value >= smallNumber)
        return smallNumberDescription;
    else
        return "";
}

2) I noticed losts of code uses the following format when writting if ... else if statements:

if ...
else if ...
else ...

But isn't ( at least conceptually ) more correct way:

if ...
else
  if ...
  else ...
like image 509
user437291 Avatar asked Sep 02 '10 19:09

user437291


People also ask

What is if-else if used for?

The if/else if statement allows you to create a chain of if statements. The if statements are evaluated in order until one of the if expressions is true or the end of the if/else if chain is reached. If the end of the if/else if chain is reached without a true expression, no code blocks are executed.

What is the difference between if and if-else?

Notice that the difference between IF statement and IF-ELSE statement is that there's an additional ELSE attached to it. The IF statement is executed if the statement inside the test condition evaluates to true; otherwise the statements inside the ELSE block is executed.

Why use else if instead of just if?

The main reason to use else if is to avoid excessive indentation. Of course both of the pieces of code above are equivalent (which means it's impossible for the latter to be mandatory other than in style guides).

Do I have to use else if?

The elseif and else blocks are optional. The statements execute only if previous expressions in the if... end block are false. An if block can include multiple elseif blocks.


2 Answers

  1. You should probably use the first. It's more accepted, and more logical. (You don't care about anything afterwards if this condition is true... indicate that in your code.)
  2. It's generally more accepted, and more compact and readable, to use the first way (else if). Python even has a specific keyword for it (elif).
like image 114
froadie Avatar answered Sep 20 '22 03:09

froadie


I personally would use

protected string GetNumberDescription(int value)
   {
       if (value >= veryBigNumber)
           return veryBigNumberDescription;
       if (value >= bigNumber)
           return bigNumberDescription;
       if (value >= smallNumber)
           return smallNumberDescription;
       return string.Empty;
   }

It really all depends on what your function is doing. If its simple like this then keep it simple. If you might need to do something with the result before you return it then I'd use Egrunin's solution

like image 34
Gage Avatar answered Sep 21 '22 03:09

Gage