Go-logical-operators
提供:Dev Guides
Go-論理演算子
次の表に、Go言語でサポートされているすべての論理演算子を示します。 変数 A が1を保持し、変数 B が0を保持すると仮定します-
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A && B) is false. |
Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A | |
B) is true. | ! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
次の表は、Go言語でサポートされているすべての論理演算子を示しています。 変数 A が真を保持し、変数 B が偽を保持すると仮定します-
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are false, then the condition becomes false. | (A && B) is false. |
Called Logical OR Operator. If any of the two operands is true, then the condition becomes true. | (A | |
B) is true. | ! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. |
例
Goプログラミング言語で使用可能なすべての論理演算子を理解するには、次の例を試してください-
package main
import "fmt"
func main() {
var a bool = true
var b bool = false
if ( a && b ) {
fmt.Printf("Line 1 - Condition is true\n" )
}
if ( a || b ) {
fmt.Printf("Line 2 - Condition is true\n" )
}
/*lets change the value of a and b*/
a = false
b = true
if ( a && b ) {
fmt.Printf("Line 3 - Condition is true\n" )
} else {
fmt.Printf("Line 3 - Condition is not true\n" )
}
if ( !(a && b) ) {
fmt.Printf("Line 4 - Condition is true\n" )
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true