Operators & If Else — GoLanguage

Always learning
3 min readJun 6, 2024

Conditional statements: These statements allow the programmer to make decisions based on the value of a variable or expression. The most common conditional statements.

Conditional statements to execute code based on different conditions. We can also use if, else if and if else statements to execute code provided if a condition is true or false.

  • The if statement — executes some code if one condition is true
  • The if…else statement — executes some code if a condition is true and another code if that condition is false

buymeacoffee ☕ 👈 Click the link

Example 1

package main

import "fmt"

func main() {
var num int
fmt.Printf("Enter a number : ")
fmt.Scanf("%d", &num)
if num%2 == 0 {
fmt.Printf("%d is Even", num)
} else {
fmt.Printf("%d is Odd", num)
}
}

Printf and Scanf are two standard C programming language functions for input and output.

Example 2

package main

import "fmt"

func main() {
var num int
fmt.Printf("Enter a number : ")
fmt.Scanf("%d", &num)
n := num / 2
if num%2 == 0 {
fmt.Printf("%d is Even, %d times", num, n)
} else {
fmt.Printf("%d is Odd, %d times", num, n)
}
}

Short Variable Declaration Operator (:=) in Golang is used to create the variables having a proper name and initial value.

Example 3

package main

import "fmt"

func main() {
var num int
fmt.Printf("Enter a number : ")
fmt.Scanf("%d", &num)
if n := num / 2; num%2 == 0 {
fmt.Printf("%d is Even, %d times", num, n)
} else {
fmt.Printf("%d is Odd, %d times", num, n)
}
}

if num % 2 == 0. means number divide 2 , and the remainder is 0, then it will print out.

Example 4

package main

import (
"fmt"
"strconv"
)

func main() {
var num int
fmt.Printf("Enter a number : ")
fmt.Scanf("%d", &num)
fmt.Printf("%s", fizzBuzz(num))

}

func fizzBuzz(n int) string {
if n%3 == 0 && n%5 == 0 {
return "FizzBuzz"
} else if n%3 == 0 {
return "Fizz"
} else if n%5 == 0 {
return "Buzz"
}
return strconv.Itoa(n)
}

Fizz and Buzz refer to all number that’s a multiple of 3 and 5 respectively. In other words, if a number is divisible by 3, it is categorized as fizz; if a number is divisible by 5, it is categorized as buzz.

If a number is simultaneously a multiple of 3 AND 5, the number is replaced with “fizz buzz.” In essence, it emulates the famous children game “fizz buzz.”

Binary Operators

Addition (+)

Substraction (-)

Multiplication (*)

Division (/)

Modulo division (%)

Bitwise Operators

bitwise AND (&)

bitwise OR (|)

bitwise XOR (^)

bit clear (AND NOT) (&^)

left shift (<<)

right shift (>>)

Comparison Operators

equal to (==)

not equal to (!=)

greater than (>)

less than (<)

greater than (or) equal to (≥)

less than (or) equal to (≤)

Thank you 🙏 for taking the time to read our blog.

--

--

Always learning

கற்றுக் கொள்ளும் மாணவன்...