For faster navigation, this Iframe is preloading the Wikiwand page for 短路求值.

短路求值

短路求值(Short-circuit evaluation; minimal evaluation; McCarthy evaluation; 又称最小化求值[1],是一种逻辑运算符的求值策略。只有当第一个运算数的值无法确定逻辑运算的结果时,才对第二个运算数进行求值。例如,当AND的第一个运算数的值为false时,其结果必定为false;当OR的第一个运算数为true时,最后结果必定为true,在这种情况下,就不需要知道第二个运算数的具体值。在一些语言中(如Lisp),默认的逻辑运算符就是短路运算符,而在另一些语言中(如JavaAda),短路和非短路的运算符都存在。对于一些逻辑运算,如XOR,短路求值是不可能的。

短路表达式x AND y,事实上等价于条件语句:if x then y else false。短路表达式x OR y,则等价于条件语句:if x then true else y

C语言和C++

[编辑]

C語言和C++語言標准強制規定了||&&短路求值語義以及求值順序。從而,下述這樣的包含安全檢查的代碼是非常常見的:

   char* pChar = 0;
   // some actions which may or may not set pChar to something
   if ((pChar != 0) && (*pChar != '\0')) {
      // do something useful
   }

C99語言標準的Section 6.5.13 Logical AND operator規定:

   (4). Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated.
   翻譯:於二元位與運算符&不同,&&運算符保證從左到右求值,第一操作數求值後有一個順序點。如果第一操作數比較等於0,則第二操作數不再求值。

類似地,section 6.5.14 Logical OR operator有類似規定。C++語言標准有同樣規定。但如果重載了||&&运算符,則僅是普通運算符。

考慮以下使用C语言寫的例子:

int a = 0;
if (a && myfunc(b)) {
    do_something();
}

在這個例子中,最小化計算使得myfunc(b)永遠不會被調用。這是因為 a 等於false,而false AND q無論q是什麼總是得到false。這個特性允許兩個有用的編程結構。首先,不論判別式中第一個子判別語句要耗費多昂貴的計算,總是會被執行,若此時求得的值為 false,則第二個子判別運算將不會執行,這可以節省來自第二個語句的昂貴計算。再來,這個結構可由第一個子判別語句來避免第二個判別語句不會導致運行時錯誤。例如對以下使用C语言寫的例子而言,最小化計算可以避免對空指针進行存取。

void * p = NULL;
int ret;
/* ... */
if(p && ret = func(p) ){
    /* 或者另一種更清晰的寫法是if( (p != NULL) && (ret = func(p)) ) */
    /* ... */
}
/* ... */

當使用最小化計算時,很重要的一點是得知表示式取值的順序。某些編程語言中確保有一致的取值順序。例如:C语言JavaPerlPythonRuby等。

它不過是下面語句的一種更加緊湊的表示形式罷了。

if (cond_a) {
    if (expensive_or_dangerous_cond_b) {
        ...
    }
}

註釋

[编辑]
  1. ^ Peter Hofer, Peter Fischer: Lexikon der Informatik. 15. Auflage. Springer, Berlin 2010, ISBN 3-642-15125-6, S. 81 (eingeschränkte Vorschau in der Google-Buchsuche).

參考文獻

[编辑]
  • Michael L. Scott: Programming Language Pragmatics. 3. Auflage. Elsevier LTD, Oxford 2009, ISBN 0-12-374514-4, S. 239 (eingeschränkte Vorschau in der Google-Buchsuche).

参閲

[编辑]

外部連結

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
短路求值
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?