0
0

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.

「A Tour of Go」でGo言語を学ぶ(~#20)

Last updated at Posted at 2012-09-25

今回はfor文です。
14回の定数はcharacter、string、boolean、数値にconstキーワードをつけるだけなので、省略する。
15回は多分ビット演算のことをやっていて、ようわからんので飛ばす。

for.go
package main

import "fmt"

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

結果


45

感想


普通にfor文。ただし、括弧()がない。つけちゃいけないみたい。
あとは条件を1つだけにして、

if2.go
package main

import "fmt"

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

という風に書けることもCやJavaと変わらないらしい。上のコードはセミコロンを省略して、

for sum < 1000{
}

とかけてしまうみたい。コレのおかげでwhile文はこの、セミコロンを省略した形で書くらしい。
無限ループは、まとめるとこのように書ける。

for {
}
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?