0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

for文

Last updated at Posted at 2019-06-18

##基本

func main() {
	for i := 0; i < 10; i++ {
		fmt.Println(i)
	}
}

###continueを使ってスキップさせる

func main() {
	for i := 0; i < 10; i++ {
		if i == 3 {
			fmt.Println("continue")
			continue
		}
		fmt.Println(i)
	}
}

###breakを使って中断させる

func main() {
	for i := 0; i < 10; i++ {
		if i == 3 {
			fmt.Println("continue")
			continue
		}
		if i > 5 {
			fmt.Println("break")
			break
		}
		fmt.Println(i)
	}
}

##for文の省略した書き方2つ
###その1

func main() {
	sum := 1
	for; sum < 10; {
		sum += sum
		fmt.Println(sum)
	}
	fmt.Println(sum)
}

###その2(セミコロン省略)

func main() {
	sum := 1
	for sum < 10 {
		sum += sum
		fmt.Println(sum)
	}
	fmt.Println(sum)
}

##その他
###無限ループ

func main() {
	sum := 1
	for sum < 10 {
		sum = 0
		fmt.Println(sum)
	}
	fmt.Println(sum)
}
func main() {
	for {
		fmt.Println("infinite loop")
	}
}

【参考】
現役シリコンバレーエンジニアが教えるGo入門(https://www.udemy.com/share/100BhMB0obeFpbTX4=/)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?