Do else if statements exist in C#?

前端 未结 8 1566
感动是毒
感动是毒 2021-02-01 12:51

I have come across the following code in C#.

if(condition0) statement0;
else if(condition1) statement1;
else if(condition2) statement2;
else if(condition3) state         


        
相关标签:
8条回答
  • 2021-02-01 13:36

    It's a multi-layered if-else.

    The reason it is has to do with c# syntax rules. An else is followed by a statement, and any if chain qualifies as a statement.

    0 讨论(0)
  • 2021-02-01 13:36

    To expand on @hunter's answer the reason, as you hit on it that without brackets it will only execute the next line, if it were a bunch of nested the else would need brackets:

    if(condition0) 
      statement0;
    else
    {
      if(condition1)
        statement1;
      else
      {
        if(condition2)
          statement2;
        else
        {
          if(condition3)
            statement3;
          else
          ...
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题