Table of contents
Control Flow
if-elseif
if condition {
// executes when condition is true
}
main.go
package main
import "fmt"
func main() {
var a string = "happy"
if a == "happy" {
fmt.Println(z)
}
}
>>> go run main.go
happy
if-else
if condition {
// executes when condition is true
} else {
// executes when condition is false
}
main.go
package main
import "fmt"
func main() {
var fruit string = "grapes"
if fruit == "apples" {
fmt.Println("Fruit is apple")
} else {
fmt.Println("Fruit is not apple")
}
>>> go run main.go
Fruit is not apple
if-else-if else
if condition_1 {
// execute when condition_1 is true
} else if condition_2 {
/* execute when condition_1 is false, and condition_2 is true */
} else if condition_3 {
/* execute when condition_1 and 2 are false, and condition_3 is true */
} else {
// when none of the above conditions are true
}
main.go
package main
import "fmt"
func main() {
fruit := "grapes"
if fruit == "apple"
fmt.Println("I love apples")
} else if fruit == "orange" {
fmt.Println("Oranges are not apples")
} else {
fmt.Println("no appetite")
}
}
>>> go run main.go
no appetite
Switch Statement
switch-case
syntax
switch expression {
case value_1:
// execute when expression equals to value_1
case value_2:
// execute when expression equals to value_2
default:
// execute when no match is found
}
main.go
package main
import "fmt"
func main() {
var i int = 100
switch i {
case 10:
fmt.Println("i is 10")
case 100, 200:
fmt.Println("i is either 100 or 200")
default:
fmt.Println("i is neither 0, 100 or 200")
}
}
>>> go run main.go
i is either 100 or 200
fall through
- The fall through keyword is used in switch-case to force the executiom flow to fall through the successive case block.
package main
import "fmt"
func main() {
var i int = 100
switch i {
case 10:
fmt.Println("i is 10")
case 100, 200:
fmt.Println("i is either 100 or 200")
default:
fmt.Println("i is neither 0, 100 or 200")
}
}
>>> go run main.go
i is either 100 or 200
package main
import "fmt"
func main() {
var i int = 10
switch i {
case -5:
fmt.Println("-5")
case 10:
fmt.Println("10")
fallthrough
case 20:
fmt.Println("20")
fallthrough
default:
fmt.Println("default")
}
}
>>> go run main.go
i is either 100 or 200
switch with condition
package main
import "fmt"
func main() {
var a, b int = 10, 20
switch {
case a+b == 30:
fmt.Println("equal to 30")
case a+b <= 30:
fmt.Println("less than or equal to 30")
default:
fmt.Println("greater than 30")
}
}
>>> go run main.go
equal to 30
looping with for loop
A loop is a sequence of instructions that is continually repeated until a certain condition is reached.
loop
fmt.Println("Hello World!")
fmt.Println("Hello World!")
fmt.Println("Hello World!")
for loop syntax:
for initialization; condition; post {
// statement
}
for loop
for i := 1; i <=3; i++ {
fmt.Println("Hello World")
}
main.go
package main
import "fmt"
func main() {
i := 1
for i<= 5 {
fmt.Println(i * i)
i +=c1
}
}
>>> go run main.go
1
4
9
16
25
main.go
package main
import "fmt"
func main() {
sum := 0
for {
sum++ // repeated forever
}
fmt.Println(sum) // never reached
}
>>> go run main.go
the infinite loop
Break statement
- the break statement end the loop immediately when it is encountered.
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
if i == 3 {
break
}
fmt.Println(i)
}
}
>>> go run main.go
1
2
continue statement
- the continue statement skips the current iteration of loop and continues with the next iteration.
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
if i == 3 {
continue
}
fmt.Println(i)
}
}
>>> go run main.go
1
2
3
4
5
That's great if you have make till here you have covered basic of Golang Control flow
If you liked what you read, do follow and any feedback for further improvement will be highly appreciated!
Thank you and Happy Learning!👏