Go - Logical Operators


Advertisements

The following table lists all the logical operators supported by Go language. Assume variable A holds 1 and variable B holds 0, then −

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

The following table shows all the logical operators supported by Go language. Assume variable A holds true and variable B holds false, then −

Operator Description Example
&& Called Logical AND operator. If both the operands are false, then the condition becomes false. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is true, then the condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true.

Example

Try the following example to understand all the logical operators available in Go programming language −

package main

import "fmt"

func main() {
   var a bool = true
   var b bool = false
   if ( a && b ) {
      fmt.Printf("Line 1 - Condition is true\n" )
   }
   if ( a || b ) {
      fmt.Printf("Line 2 - Condition is true\n" )
   }
   
   /* lets change the value of  a and b */
   a = false
   b = true
   if ( a && b ) {
      fmt.Printf("Line 3 - Condition is true\n" )
   } else {
      fmt.Printf("Line 3 - Condition is not true\n" )
   }
   if ( !(a && b) ) {
      fmt.Printf("Line 4 - Condition is true\n" )
   }
}

When you compile and execute the above program it produces the following result −

Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
go_operators.htm
Advertisements