[Golang] Introduction to Control Flow

If statement

if (condition1) {

} else if (condition2) {

} else {

}

Note that, the parentheses () can be omitted from an if statement. for example:

package main

import "fmt"

func main() {
	var x = 7
	if num%3 == 0 && num%5 == 0 {
		fmt.Printf("%d is divisible by both 3 and 5\n", x)
	} else if x < 0 {
		fmt.Printf("%d is negative\n", x)
	} else {
		fmt.Printf("%d is positive\n", x)
	}
}

Switch statement

Switch evaluates all the cases from top to bottom until a case succeeds. Once a case succeeds, it runs the block of code specified in that case and then stops the evaluation.

package main

import "fmt"

func main() {
	code := "AT"
	switch code {
	case "TW", "ROC":
		fmt.Println("Taiwan")
	case "AU":
		fmt.Println("Australia")
	case "AT":
		{
			fmt.Println("Austria")
			fmt.Println("There is no kangaroos in Austria")
		}
	default:
		fmt.Println("Other Country")
	}
}

For loop

You can use continue statement to skip running the loop body midway and continue to the next iteration of the loop.
The break statement can be used to stop a loop before its normal termination.

package main

import "fmt"

func main() {
	for i := 100; i < 150; i++ {
		if i%2 == 0 {
			continue
		} else if i%7 == 0 {
			fmt.Printf("%d is a the first multiple of 7\n", i)
			break
		}
		fmt.Printf("%d is a the odd number\n", i)
	}
}

<<:  IT铁人DAY 1-进入物件导向世界前的心理准备

>>:  [Day02] 程序菜鸟自学C++资料结构 – 简单QA

第三天 Routes 与 MVC

呈前一天的问题!昨日的答案是因为我们有在 yml 档设定 production 的环境要使用 pgs...

[Day 12] 决策树 (Decision tree)

决策树 (Decision tree) 今日学习目标 决策树演算法介绍 决策树如何生成? 如何处理分...

[Day 35] 自我介绍後台及前台(四) - 前台的首页

我们终於开始写部落格前台的部分, 我们先将首页改成所有人的图像列表, 然後点进去可以看到这个使用者的...

BPM懒人包 让你一次搞懂BPM的大小事

为了要了解企业流程管理(BPM),很多人上网搜寻到的文章,常常都有些八股,或是看不到想要了解的部分,...

Day#04 TableView

前言 承接昨天的内容,今天来加上viewDidLoad中的逻辑内容{ @•̀ꈊ•́@ } 学习资源 ...