如果 IF 语句不满足第一个条件,它会停止评估吗? [英] Will an IF statement stop evaluating if it fails the first condition?

查看:16
本文介绍了如果 IF 语句不满足第一个条件,它会停止评估吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有 2 个条件的 If 语句 - 第一个条件失败,是否会考虑第二个条件,还是会直接进入 else?因此,在以下示例中,如果 myList.Count == 0,将 myString 与值"进行比较还是直接与 else?

If I have an If statement with 2 conditions - and the first fails, will the 2nd condition even be considered or will it go straight to the else? So, in the following example, if myList.Count == 0, will myString be compared against "value" or will it just straight to else?

if(myList.Count > 0 && myString.Equals("value"))
{
//Do something
}
else
{
//Do something else
}

推荐答案

它将停止评估,因为您使用的是双与号 &&操作员.这称为 短路.

It will stop evaluating because you're using the double ampersand && operator. This is called short-circuiting.

如果您将其更改为单个 & 符号:

If you changed it to a single ampersand:

if(myList.Count > 0 & myString.Equals("value"))

它会同时评估两者.

这篇关于如果 IF 语句不满足第一个条件,它会停止评估吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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