Algorithm


  1. In Go, booleans are a fundamental data type representing true or false values. Go provides relational and logical operators to work with boolean values.

    Relational Operators:

    1. Equality (==):

      go
      a == b
  2.  

    Checks if the values of a and b are equal.

  3. Inequality (!=):

    go
    a != b
  4.  

    Checks if the values of a and b are not equal.

  5. Greater Than (>):

    go
    a > b
  6.  

    Checks if the value of a is greater than the value of b.

  7. Less Than (<):

    go
    a < b
  8.  

    Checks if the value of a is less than the value of b.

  9. Greater Than or Equal To (>=):

    go
    a >= b
  10.  

    Checks if the value of a is greater than or equal to the value of b.

  11. Less Than or Equal To (<=):

    go
    a <= b
    1.  

      Checks if the value of a is less than or equal to the value of b.

    Logical Operators:

    1. Logical AND (&&):

      go
      a && b
  12.  

    Returns true if both a and b are true.

  13. Logical OR (||):

    go
    a || b
  14.  

    Returns true if either a or b is true.

  15. Logical NOT (!):

    go
    !a
  16.  

    Returns true if a is false, and false if a is true.

 

Code Examples

#1 Go Boolean Data Types

Code - Golang Programming

package main
import "fmt"

func main() {

  var boolTrue bool = true
  var boolFalse bool = false

  fmt.Println("The boolean values are", boolTrue, "and", boolFalse)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
The boolean values are true and false

#2 Relational Operators in Golang Pragramming

Code - Golang Programming

number1 := 9
number2 := 3

result := number1 > number2
Copy The Code & Try With Live Editor

#3 Relational Operator in Go Lang Programming

Code - Golang Programming

// Program to illustrate the working of Relational Operators

package main
import "fmt"

func main() {

  number1 := 12
  number2 := 20
  var result bool

  // equal to operator
  result = (number1 == number2)

  fmt.Printf("%d == %d returns %t \n", number1, number2, result)

  // not equal to operator
  result = (number1 != number2)

  fmt.Printf("%d != %d returns %t \n", number1, number2, result)

  // greater than operator
  result = (number1 > number2)

  fmt.Printf("%d > %d returns %t \n", number1, number2, result)

  // less than operator
  result = (number1 < number2)

  fmt.Printf("%d < %d returns %t \n", number1, number2, result)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
12 == 20 returns false
12 != 20 returns true
12 > 20 returns false
12 < 20 returns true

#4 Logical Operator in Go Lang Programming

Code - Golang Programming

// Program to illustrate the working of Logical Operator

package main
import "fmt"

func main() {

  number1 := 6
  number2 := 12
  number3 := 6
  var result bool

  // returns false because number1 > number2 is false
  result = (number1 > number2) && (number1 == number3)

  fmt.Printf("Result of AND operator is %t \n", result)

  // returns true because number1 == number3 is true
  result = (number1 > number2) || (number1 == number3)

  fmt.Printf("Result of OR operator is %t \n", result)
  
  // returns false because number1 == number3 is true
  result = !(number1 == number3);

  fmt.Printf("Result of NOT operator is %t \n", result)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Result of AND operator is false
Result of OR operator is true
Result of NOT operator is false
Advertisements

Demonstration


Go Booleans (Relational and Logical Operators)-Go Lang Programming