if / else和if / elseif [英] if/else and if/elseif

查看:168
本文介绍了if / else和if / elseif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的声明块:

If I have a statement block like this:

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

或者像这样:

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

有什么区别?

似乎使用if / else,如果part为true状态,​​else部分为所有其他可能选项(false)。一个else-if对许多条件都有用。这是我的理解,还有什么我应该知道的吗?

It seems that with if/else, if part is for true state and the else part is for all other possible options (false). An else-if would be useful for a number of conditions. This is my understanding, is there anything more I should be aware of?

推荐答案

情况a:

if( condition )
{
}
else
{
}

当上述语句中的条件为false时,将始终执行else块中的语句。

When the condition in the above statement is false, then the statements in the else block will always be executed.

情况b:

if( condition )
{
}
else if( condition2 )
{
}
else
{
}

当'condition'为false时,else if块中的语句只会在condition2为true时执行。
当condition2为false时,else块中的语句将被执行。

When 'condition' is false, then the statements in the else if block will only be executed when condition2 is true. The statements in the else block will be executed when condition2 is false.

这篇关于if / else和if / elseif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆