Control Flow - If/Else
Below is the sample code demonstrating If/Else
package main
import (
"fmt"
)
func main() {
var isBool bool
var isBig bool = true
fmt.Println("Value of unassigned var isBool is -->", isBool)
fmt.Println("Value of assigned var isBig is -->", isBig)
fmt.Println("Value of Logical AND of true && false is --> ", isBool && isBig)
fmt.Println("Value of Logical OR of true || false is --> ", isBool || isBig)
if toggle := false; toggle {
fmt.Println("Value of toggle is --> ON -- True")
} else {
fmt.Println("Value of toggle is --> OFF -- False")
}
}
Control Flow - For
Below is the sample code demonstrating for loop
package main
import "fmt"
func main() {
x := 0
y := 0
var xbreak int = 3
for {
if x++; x > xbreak {
fmt.Println("Value of x at break after it is increased more than 3 -->", x)
break
}
fmt.Println("The moment value of x --> ", x)
}
fmt.Println("\n")
for y < 3 {
fmt.Println("Value of Y after incrementing it and as long as it is less than 3 -->", y)
y++
}
fmt.Println("\n")
for z := 0; z < 10; z++ {
if z < 8 {
continue
}
fmt.Println("Value of z is as long as it is lt 10 is --> ", z)
}
}
Control Flow - Switch
Below is the sample code demonstrating switch
package main import "fmt" func main() { switch signal := 2; signal { case 0: //here the signal value is evaluated for 0 fmt.Println("Red as the value of Signal is --> ", signal) case 1: //here the signal value is evaluated for 1 fmt.Println("Green as the value of Signal is --> ", signal) case 2: //here the signal value is evaluated for 2 fmt.Println("Orange as the value of Signal is --> ", signal) } var score int = 100 switch { case score < 50: fmt.Println("Beginner") case score > 50 && score <= 75: fmt.Println("Advanced") default: fmt.Println("Expert") } }
No comments:
Post a Comment