if/else and if/elseif

后端 未结 10 1886
傲寒
傲寒 2020-12-17 01:00

If I have a statement block like this:

if (/*condition here*/){ }
else{ }

or like this:

if (/*condition here*/)
else if (/         


        
10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 01:49

    You already gave the answer yourself. if/else is for true/false result, like is an int=2 or any other possible int value, and if/elseif is for more than 2 results, like an int=2, and int=3 and so on.

    Also it groups the context of a variable. You could check every single result like

    if (a == 2) { do one thing };
    if (a == 3) { do another thing };
    ...
    if (a != 2 && a != 3 ...) { do something else };
    

    With if/else/elseif it's better readable.

提交回复
热议问题