ADVERTISEMENT
ADVERTISEMENT

Golang Logical Operators

Here, we are going to learn about the Logical Operators in the Go programming language with examples.
Submitted by IncludeHelp, on December 07, 2021

Logical Operators

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 Golang 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 Golang 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

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.