Tuesday, September 8, 2020

Golang: Control statements

 


If,else if and else
//Control statements
package main

import "fmt"

func main() {
	x := 9
	if x == 9 {
		fmt.Println("x is equal to 9")
	}
	if y := 18; y == 18 {
		fmt.Println("y is equal to 18")
	} else {
		fmt.Println("y is not equal to 18")
	}

	z := 36
	if z == 1 {
		fmt.Println("z is equal to 1")
	} else if z == 2 {
		fmt.Println("z is equal to 2")
	} else if z == 3 {
		fmt.Println("z is equal to 3")
	} else {
		fmt.Println("z doesn not equal 1,2,or 3")
	}
}

Switch statement
//Control statements
package main

import "fmt"

func main() {
	z := 36
	switch z {
	case 1:
		fmt.Println("z is equal to 1")
	case 2:
		fmt.Println("z is equal to 2")
	case 3:
		fmt.Println("z is equal to 3")
	default:
		fmt.Println("z is equal to 1,2, or 3")
	}
}

No comments:

Post a Comment