Go break和continue

1. break

package main
import (
	"fmt"
	"math/rand"
	"time"
)

func main() {

	// we have to generate a random number, you also need a rand to set up a seed.
	//time.Now () Unix ():. returns from 1970: 01 seconds 0:00:00 to now: 01
	//rand.Seed(time.Now().Unix())
	// how to generate random integer 1-100
	//n := rand.Intn(100) + 1 // [0 100)
	//fmt.Println(n)

	// generate a random number from 1 to 100, until the number 99 is generated, see if you share a few times
	//analysis of idea:
	// write an infinite loop control, and then stop randomly generated number, is generated when the 99, exits the infinite loop == "break
	var count int = 0
	for {
		rand.Seed(time.Now().UnixNano())
		n := rand.Intn(100) + 1
		fmt.Println("n=", n)
		count++
		if (n == 99) {
			break // represents out of the for loop
		}
	}

	fmt.Println ( "generated using a total of 99", count)


	// show you here in the form of the specified label to use break
	lable2: 
	for i := 0; i < 4; i++ {
		// lable1: // set up a label
		for j := 0; j < 10; j++ {
			if j == 2 {
				// break // break out of recent default for loop
				//break lable1 
				break lable2 // j=0 j=1
			}
			fmt.Println("j=", j) 
		}
	}
}

  

Guess you like

Origin www.cnblogs.com/yzg-14/p/12178840.html