1 package main
2
3 import "fmt"
4
5 func main() {
6
7 /*
8 逻辑运算符:操作数必须是bool,运算结果也是bool
9 逻辑与:&&
10 运算规则:所有的操作数都是真,结果才为真,有一个为假,结果就为假
11 "一假则假,全真才真"
12 逻辑或:||
13 运算规则:偶有的操作数都是假,结果才为假,有一个为真,结果就为真
14 "一真则真,全假才假"
15 逻辑非:!
16 !T-->false
17 !F-->true
18 */
19
20 f1 := true
21 f2 := false
22 f3 := true
23 res1 := f1 && f2
24 fmt.Printf("res1: %t\n", res1) // res1: false
25
26 res2 := f1 && f2 && f3
27 fmt.Printf("res2: %t\n", res2) // res2: false
28
29 res3 := f1 || f2
30 fmt.Printf("res3: %t\n", res3) // res3: true
31 res4 := f1 || f2 || f3
32 fmt.Printf("res4: %t\n", res4) // res4: true
33 fmt.Println(false || false || false) // false
34
35 fmt.Printf("f1:%t,!f1:%t\n", f1, !f1) // f1:true,!f1:false
36 fmt.Printf("f2:%t,!f2:%t\n", f2, !f2) // f2:false,!f2:true
37
38 a := 3
39 b := 2
40 c := 5
41 res5 := a > b && c%a == b && a < (c/b)
42 fmt.Println(res5) // false
43
44 res6 := b*2 < c || a/b != 0 || c/a > b
45 fmt.Println(res6) // true
46 res7 := !(c/a == b)
47 fmt.Println(res7) // true
48
49 }