Home »
Golang
Golang Logical Operators
Last Updated : April 19, 2025
Logical operators are used for making decisions based on multiple conditions, they are used to check multiple conditions, i.e., the logical operators are the symbols used to connect two or more expressions/conditions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. The common logical operators are Logical AND (&&), Logical OR (||), and Logical NOT (!).
List of Logical Operators
Operator |
Description |
Example x:=5 y:=2 |
Logical AND (&&) |
It returns true if all operands are true; false, otherwise. |
x==5 && y==2 returns true |
Logical OR (||) |
It returns true if any of the operands is true; false, otherwise. |
x==5 || y==2 returns true |
Logical NOT (!) |
It returns true if the operand is false; false, otherwise. |
!(x==5) returns false |
Example of Logical Operators
The below Golang program is demonstrating the example of logical operators.
// Golang program demonstrate the
// example of logical operators
package main
import "fmt"
func main() {
x := 5
y := 2
var result bool
result = (x == 5 && y == 2)
fmt.Println("result:", result)
result = (x == 5 || y == 2)
fmt.Println("result:", result)
result = !(x == 5)
fmt.Println("result:", result)
}
Output:
result: true
result: true
result: false
Go Logical Operators Exercise
Select the correct option to complete each statement about logical operators in Go.
- The operator ___ is used to perform a logical AND operation in Go.
- In Go, the operator ___ is used to perform a logical OR operation.
- The operator ___ is used to perform a logical NOT operation in Go.
Advertisement
Advertisement