如果如果结构的第一部分是假会发生什么? [英] What happens if the first part of an if-structure is false?

查看:169
本文介绍了如果如果结构的第一部分是假会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,当一个程序处理的,如果结构有多个条件会发生什么。我有一个想法,但我不知道这件事。我举一个例子:

I was wondering what happens when a program processes an if-structure with multiple conditions. I have an idea, but I'm not sure about it. I'll give an example:

List<string> myTestList = null;
if (myTestList != null && myTestList.Count > 0)
{
    //process
}

该列表为空。当处理if语句,将它从左边到右边,如果只要有一个条件是假的退出?

The list is null. When processing the if statement, will it go from left to right exiting the if as soon as one condition is false?

我已经尝试过了,似乎扔没有错误,所以我想上面的解释,但我不知道。

I've tried it and seems to throw no errors, so I assume the above explains it, but I'm not sure.

推荐答案

这是 &功放;&安培; 这是非常重要的。这是短路,因此计数不会求;该条件是从左向右

It is the && that is important. This is short-circuiting, so the Count is never evaluated; The conditions are evaluated left-to-right.

还有一个非短路操作符(&安培; ),但它的非常罕见的在看是否测试; 。它主要用于位操作(在 INT 等)

There is also a non-short-circuiting operator (&), but it is very rare to see in an if test; it is mainly intended for bitwise operations (on int etc).

从规格:

条件逻辑运算符

&放;&安培; || 符被称为条件逻辑运算符。
它们也称为
短路逻辑运算符。

The && and || operators are called the conditional logical operators. They are also called the "short-circuiting" logical operators.

...

&放大器;&安培; || 运营商都是有条件的版本的 &安培; |
运营商:

The && and || operators are conditional versions of the & and | operators:


  • 操作 X&功放;&安培;是对应于操作 X'放大器;是,但
    只计算,如果 X 不是

  • 的操作 X ||是对应于操作 X |是,但
    只计算,如果 X 不是真正

  • The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false.
  • The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.

这篇关于如果如果结构的第一部分是假会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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