Go Logical Operators

Introduction

Logical operators are used to combine or invert boolean values. In Go, logical operators are essential for controlling the flow of your programs by evaluating conditions. In this chapter, you will learn the different logical operators in Go, with examples for each type.

Logical Operators in Go

Logical AND (&&)

The logical AND operator returns true if both operands are true. If either operand is false, it returns false.

Syntax:

result = operand1 && operand2

Example:

package main

import "fmt"

func main() {
    var a bool = true
    var b bool = false
    var result bool = a && b
    fmt.Println("a && b:", result) // Output: a && b: false
}

Logical OR (||)

The logical OR operator returns true if at least one of the operands is true. If both operands are false, it returns false.

Syntax:

result = operand1 || operand2

Example:

package main

import "fmt"

func main() {
    var a bool = true
    var b bool = false
    var result bool = a || b
    fmt.Println("a || b:", result) // Output: a || b: true
}

Logical NOT (!)

The logical NOT operator inverts the boolean value of its operand. If the operand is true, it returns false. If the operand is false, it returns true.

Syntax:

result = !operand

Example:

package main

import "fmt"

func main() {
    var a bool = true
    var result bool = !a
    fmt.Println("!a:", result) // Output: !a: false
}

Combining Logical Operators

Logical operators can be combined to form complex boolean expressions.

Example:

package main

import "fmt"

func main() {
    var a bool = true
    var b bool = false
    var c bool = true

    var result bool = (a && b) || c
    fmt.Println("(a && b) || c:", result) // Output: (a && b) || c: true
}

Logical Operators with Relational Operators

Logical operators are often used with relational operators to evaluate complex conditions.

Example:

package main

import "fmt"

func main() {
    var a int = 5
    var b int = 3
    var c int = 8

    var result bool = (a > b) && (c > a)
    fmt.Println("(a > b) && (c > a):", result) // Output: (a > b) && (c > a): true
}

Short-Circuit Evaluation

Go uses short-circuit evaluation for logical operators. This means that evaluation stops as soon as the result is determined.

Example:

package main

import "fmt"

func main() {
    var a bool = false
    var b bool = true

    // The second condition (b) is not evaluated because the first condition (a) is false
    if a && b {
        fmt.Println("Both a and b are true")
    } else {
        fmt.Println("Either a or b is false") // Output: Either a or b is false
    }

    // The second condition (b) is not evaluated because the first condition (a) is true
    if a || b {
        fmt.Println("At least one of a or b is true") // Output: At least one of a or b is true
    } else {
        fmt.Println("Both a and b are false")
    }
}

Conclusion

Logical operators in Go are crucial for evaluating boolean expressions and controlling the flow of your programs. By understanding and using the logical AND (&&), logical OR (||), and logical NOT (!) operators, you can create complex conditions and make your programs more efficient and effective. Remember to take advantage of short-circuit evaluation to optimize your code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top