Break and Fallthrough in switch

break

Can be used in switch or in a for loop to end the switch branch by forcing the end of a case statement

fallthrough

When a certain case in the switch is matched successfully, the case statement is executed. If fallthrough is encountered, the next case does not need to be matched and the through-through execution is performed. Only on the last line of case.

Package main import "FMT" func main() {n:=2 switch n {case 1: FMT.Println("一") FMT.Println("一") case 2: FMT.Println("一") Println("二") break// Force end of case fmt.Println("二") case 3: FMT. Println (" three "), FMT. Println (" three ")} FMT. Println ("..." ) m:=2 switch m {case 1: FMT.Println("一") case 2: FMT.Println("一") fallthrough case 3: Println("三") fallthrough case 4: Println("三")}}Copy the code

For loop

Some code executes for expression 1 more than once; Expression is 2; Expression 3{loop body} for init; condition; post{ }Copy the code
package main import "fmt" func main() { for i:=1; i<=5; i++{ fmt.Println("hello world") } }Copy the code

Another way to write the for loop

1. Standard writing

For expression 1; Expression is 2; Expression 3{body of loop}Copy the code

2. Omit expressions 1 and 3

For expression 2{body of loop}Copy the code

3. Omit three expressions

For {loop body}Copy the code

4. Omit any expression in the for loop

Expression 1: define expression 2: judge expression 3: changeCopy the code
	for  i<5 {
		fmt.Println(i)
		i++;
	}
Copy the code

for j:=1;; { fmt.Println(j) j++ if j>5 { break } }Copy the code

	m:=1
	for  {
		fmt.Println(m)
		m++
		if m>7 {
			break
		}
	}
Copy the code