-
Operators
Logical operators in programming are like a set of tools that help us decide what to do depending on how things look. Logical operators tell the computer to do something based on how two different things compare.
-
-
Increment Operators
-
The increment operator increments the value of a single variable.
Go
Example of increment operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	// Increment
	age++
	fmt.Println(age) // 21

	// Increment After
	// NA

	// Increment Before
	// NA
}
-
-
-
Decrement Operators
-
The decrement operator decrements the value of a single variable.
Go
Example of decrement operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	// Decrement
	age--
	fmt.Println(age) // 19

	// Decrement After
	// NA

	// Decrement Before
	// NA
}

-
-
-
Addition Arithmetic Operators
-
The addition operator returns the sum of two variables.
Go
Example of addition operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age + 1) // 21
}

-
-
-
Subtraction Arithmetic Operators
-
The subtraction operator returns the difference of two variables.
Go
Example of subtraction operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age - 2) // 18
}

-
-
-
Multiplication Arithmetic Operators
-
The multiplication operator returns the product of two variables.
Go
Example of multiplication operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age * 3) // 60
}

-
-
-
Division Arithmetic Operators
-
The division operator returns the quotient of two variables.
Go
Example of division operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age / 2) // 10
}

-
-
-
Modulus Arithmetic Operators
-
The modulus operator returns the remainder of two variables.
Go
Example of modulus operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age % 9) // 2
}

-
-
-
Exponentiation Arithmetic Operators
-
The exponentiation operator returns the power of two variables.
Go
Example of exponentiation operator in go.
package main

import (
	"fmt"
	"math"
)

func main() {
	var age float64 = 2

	fmt.Println(math.Pow(age, 3)) // 8
}

-
-
-
Addition Assignment Arithmetic Operators
-
The addition assignment operator returns the sum of two counterparts.
Go
Example of addition assignment operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	age += 1

	fmt.Println(age) // 21
}
-
-
-
Subtraction Assignment Arithmetic Operators
-
The subtraction assignment operator returns the difference of two counterparts.
Go
Example of subtraction assignment operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	age -= 2

	fmt.Println(age) // 18
}
-
-
-
Multiplication Assignment Arithmetic Operators
-
The multiplication assignment operator returns the product of two counterparts.
Go
Example of multiplication assignment operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	age *= 3

	fmt.Println(age) // 60
}
-
-
-
Division Assignment Arithmetic Operators
-
The division assignment operator returns the quotient of two counterparts.
Go
Example of division assignment operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	age /= 2

	fmt.Println(age) // 10
}
-
-
-
Modulus Assignment Arithmetic Operators
-
The modulus assignment operator returns the remainder of two counterparts.
Go
Example of modulus assignment operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	age %= 9

	fmt.Println(age) // 2
}
-
-
-
Exponentiation Assignment Arithmetic Operators
-
The exponentiation assignment operator returns the power of two counterparts.
Go
Example of exponentiation assignment operator in go.
// NA
-
-
-
Equal Comparison Operators
-
The equal operator checks if two variables are equal value.
Go
Example of equal operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age == 20) // true
}

-
-
-
Equal Type Comparison Operators
-
The equal type operator checks if two variables are equal value and type.
Go
Example of equal type operator in go.
package main

import "fmt"

func main() {
	var age string = "20"

	fmt.Println(age == "20") // true
}

-
-
-
Not Equal Comparison Operators
-
The not-equal operator checks if two variables are not equal value.
Go
Example of not-equal operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age != 18) // true
}

-
-
-
Not Equal Type Comparison Operators
-
The not-equal type operator checks if two variables are not equal value and type.
Go
Example of not-equal type operator in go.
// NA
-
-
-
Greater Than Comparison Operators
-
The greater-than operator checks if the value of the first variable is greater than the second.
Go
Example of greater-than operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age > 18) // true
}

-
-
-
Less Than Comparison Operators
-
The less-than operator checks if the value of the first variable is less than the second.
Go
Example of less-than operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age < 21) // true
}

-
-
-
Greater Than or Equal Comparison Operators
-
The greater-than or equal-to operator checks if the value of the first variable is greater than or equal to the second.
Go
Example of greater-than or equal-to operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age >= 20) // true
}

-
-
-
Less Than or Equal Comparison Operators
-
The less-than or equal-to operator checks if the value of the first variable is less than or equal to the second.
Go
Example of less-than or equal-to operator in go.
package main

import "fmt"

func main() {
	var age int = 20

	fmt.Println(age <= 20) // true
}

-
-
-
And Logical / Bitwise Operators
-
The and operator checks if the value of the first and second variable are true.
Go
Example of and operator in go.
package main

import "fmt"

func main() {

	// Bitwise

	var one int = 1
	var zero int = 0

	fmt.Println(one & one) // 1
	fmt.Println(zero & one) // 0
	fmt.Println(zero & zero) // 0
	fmt.Println(one & zero) // 0

	// Logical

	var isTruthy bool = true
	var isFalsy bool = false

	fmt.Println(isTruthy && isTruthy) // true
	fmt.Println(isFalsy && isTruthy) // false
	fmt.Println(isFalsy && isFalsy) // false
	fmt.Println(isTruthy && isFalsy) // false
}
-
-
-
Or Logical / Bitwise Operators
-
The or operator checks if either the value of the first or second variable is true.
Go
Example of or operator in go.
package main

import "fmt"

func main() {

	// Bitwise

	var one int = 1
	var zero int = 0

	fmt.Println(one | one) // 1
	fmt.Println(zero | one) // 1
	fmt.Println(zero | zero) // 0
	fmt.Println(one | zero) // 1

	// Logical

	var isTruthy bool = true
	var isFalsy bool = false

	fmt.Println(isTruthy || isTruthy) // true
	fmt.Println(isFalsy || isTruthy) // true
	fmt.Println(isFalsy || isFalsy) // false
	fmt.Println(isTruthy || isFalsy) // true
}
-
-
-
Xor Logical / Bitwise Operators
-
The xor operator checks if either the value of the first or second variable is true, but not both.
Go
Example of xor operator in go.
package main

import "fmt"

func main() {

	// Bitwise

	var one int = 1
	var zero int = 0

	fmt.Println(one ^ one) // 0
	fmt.Println(zero ^ one) // 1
	fmt.Println(zero ^ zero) // 0
	fmt.Println(one ^ zero) // 1

	// Logical

	var isTruthy bool = true
	var isFalsy bool = false

	fmt.Println(isTruthy != isTruthy) // false
	fmt.Println(isFalsy != isTruthy) // true
	fmt.Println(isFalsy != isFalsy) // false
	fmt.Println(isTruthy != isFalsy) // true
}
-
-