IF语句是否会在第一个条件失败时停止评估? [英] Will an IF statement stop evaluating if it fails the first condition?

查看:181
本文介绍了IF语句是否会在第一个条件失败时停止评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有2个条件的 If 语句 - 并且第一个条件失败,那么第二个条件是否会被考虑或将直接转到其他?因此,在以下示例中,如果 myList.Count == 0 ,将 myString 与value进行比较或它会直接到否则

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天全站免登陆