zoukankan      html  css  js  c++  java
  • 逻辑运算符

    用于连接多个条件(一般来讲就是关系表达式),最终的结果也是一个bool值。

    func main() {

      //演示逻辑运算符的使用 &&
      var age int = 40
      if age > 30 && age < 50 {
        fmt.Println("OK1")    //结果是true,输出。
      }


      if age > 30 && age < 40 {
        fmt.Println("OK2")   //结果是false,不输出。
      }

      //演示逻辑运算符的使用 ||
      if age > 30 || age < 50 {
        fmt.Println("OK3")    //结果是true,输出。
      }


      if age > 30 || age < 40 {
        fmt.Println("OK4")    //结果是true,输出。
      }

      //演示逻辑运算符的使用 !
      if age > 30 {
        fmt.Println("OK5") //结果是true,输出。
      }


      if !(age > 30) {
        fmt.Println("OK6") //结果是false,不输出。
      }
    }


    注意事项和细节说明:

    1) && 也叫短路与:如果第一个条件为false,则第二个条件不会判断,最终结果为false。

    2)|| 也叫短路或:如果第一个条件为true,则第二个条件不会判断,最终结果为true。

    案例演示:

    cat main.go
    package main
    import "fmt"

    //声明一个函数(测试)

    func test() bool {
      fmt.Println("test....")
      return true
    }

    func main() {

      var i int = 10
      //短路与
      //说明:因为 i < 9 为false,因此后面的test() 就不执行
      if i < 9 && test() {
        fmt.Println("ok...")
      }

      //说明:i < 9 虽然为false,但是后面的test()函数是true的,所以最后输出的结果是 test.... 和 hello...
      if i < 9 || test() {
        fmt.Println("hello...")
      }

      //短路或
      说明:因为 i > 9 为true,因此后面的test() 就不执行了,最后输出的结果为 hello...
      if i > 9 || test() {
        fmt.Println("hello...")
      }
    }

  • 相关阅读:
    POJ 3281 Dining 网络流最大流
    Codeforces Gym 100203I I
    Codeforces Gym 100203G G
    Codeforces Gym 100203E E
    Codeforces Gym 100523K K
    Codeforces Gym 100523E E
    Codeforces Gym 100523C C
    Codeforces Codeforces Round #316 (Div. 2) C. Replacement SET
    Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
    URAL 1784 K
  • 原文地址:https://www.cnblogs.com/green-frog-2019/p/11342804.html
Copyright © 2011-2022 走看看