Swift 基礎教程:邏輯運算符(Logical Operators)

常用邏輯運算符(Logical Operators)

邏輯運算符 描述 示例
! 邏輯非 !a
|| 邏輯或 a || b
&& 邏輯與 a && b

邏輯非運算符

邏輯非運算符運算符( !a )對一個布爾值取反,使得 true 變 false , false 變 true 。
! 後面不能跟空格

let status = true
if !status {
    print("it's error")
}

// continue

邏輯或運算符

當左右兩邊的表達式其中一個爲 true 時,整個表達式就爲 true 。

let status1 = true
let status2 = false

if status1 || status2 {
    print("it's ok")
}

邏輯與運算符

當左右兩邊的表達式都爲 true 時,整個表達式就爲 true 。

let status1 = true
let status2 = false

if status1 && status2 {
    print("it's ok")
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章