我今天在javascript中閱讀了一段代碼,突然發現有這段代碼片段的情況。while(true)條件的意義是什麼?
add: function (value) {
var current = null;
if (this._root === null) {
this._root = node;
} else {
current = this._root;
while (true) {
// Some code goes here.
}
}
}
條件while(true)
永遠是對的。它沒有進入我的腦海,爲什麼當條件放在這裏。
有時,打破循環所需的邏輯非常複雜,以便用「break」語句將其放入循環體中更方便。需要額外的照顧,以確保總是有一些條件會打破循環。如果條件當然在循環的頭部,情況也是如此,但是當條件複雜時,可能會更容易錯過某些東西。 – 2013-05-04 23:55:41