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

查看:33
本文介绍了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 中,if 部分用于 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?

推荐答案

情况一:

if( condition )
{
}
else
{
}

当上述语句中的条件为假时,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'为假时,else if块中的语句只会在condition2为真时执行.当条件2为假时,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天全站免登陆