module mainfnmain() { w :=true&&trueprintln('w: $w') x :=false&&trueprintln('x: $x') y :=true&&falseprintln('y: $y') z :=false&&falseprintln('z: $z')/* w: true x: false y: false z: false */}
範例:邏輯運算符||
module mainfnmain() { w :=true||trueprintln('w: $w') x :=false||trueprintln('x: $x') y :=true||falseprintln('y: $y') z :=false||falseprintln('z: $z')/* w: true x: true y: true z: false */}
範例:邏輯運算符!
module mainfnmain() { x :=trueprintln('x: $x') y :=!xprintln('y: $y') z :=!yprintln('z: $z')/* x: true y: false z: true */}
邏輯運算的運算元皆為布林型別,而其他型別之間的關係評估可以使用關係運算符:
少於<:左側運算元的值小於右側運算元的值時為true
多於>:左側運算元的值大於右側運算元的值時為true
等於==:左側運算元的值與右側運算元的值相同時為true
不等於==:左側運算元的值與右側運算元的值不相同時為true
少於或等於<=:左側運算元的值小於或相同於右側運算元時為true
多於或等於>=:左側運算元的值大於或相同於右側運算元時為true
範例:關係運算符
module mainfnmain() {mut result :=falsemut my_coins :=1000mut friend_coins :=900println("I have $my_coins coins.")println("friend has $friend_coins coins.")// less than result = friend_coins < my_coinsprintln("friend's less than mine: $result") // true// not equal to result = friend_coins != my_coinsprintln("friend's not equal to mine: $result") // true// greater thanprintln("I give friend 200 coins.") my_coins = my_coins -200 friend_coins = friend_coins +200 result = friend_coins > my_coinsprintln("friend's greater than mine: $result") // true// equal toprintln("friend gives me 150 coins.") friend_coins = friend_coins -150 my_coins = my_coins +150 result = friend_coins == my_coinsprintln("friend's equal to mine: $result") // true// less than or equal toprintln('my coins is less than or equal to 900: ${my_coins <= 900}') // falseprintln('my coins is less than or equal to 950: ${my_coins <= 950}') // trueprintln('my coins is less than or equal to 1000: ${my_coins <= 1000}') // true// greater than or equal toprintln('my coins is greater or equal to 900: ${my_coins >= 900}') // trueprintln('my coins is greater or equal to 950: ${my_coins >= 950}') // trueprintln('my coins is greater or equal to 1000: ${my_coins >= 1000}') // false/* I have 1000 coins. friend has 900 coins. friend's less than mine: true friend's not equal to mine: true I give friend 200 coins. friend's greater than mine: true friend gives me 150 coins. friend's equal to mine: true my coins is less than or equal to 900: false my coins is less than or equal to 950: true my coins is less than or equal to 1000: true my coins is greater or equal to 900: true my coins is greater or equal to 950: true my coins is greater or equal to 1000: false */}