計算機科学のブログ

which code runs next - Conditionals and Loops - Loops

Head First GoJay McGavren(著)、O’Reilly Media)の Chapter 2(which code runs next? Conditionals and Loops)、p.65(Exercise)の解答を求めてみる。

コード

sample.go

package main

import "fmt"

func main() {
	// 321
	for x := 3; x >= 1; x-- {
		fmt.Print(x)
	}
	fmt.Println()
	// 23
	for x := 2; x <= 3; x++ {
		fmt.Print(x)
	}
	fmt.Println()
	// 12
	for x := 1; x < 3; x++ {
		fmt.Print(x)
	}
	fmt.Println()
	// 13
	for x := 1; x <= 3; x += 2 {
		fmt.Print(x)
	}
	fmt.Println()
	//
	for x := 1; x >= 3; x++ {
		fmt.Print(x)
	}
	fmt.Println()
}

入出力結果(Terminal, Zsh)

% go run ./sample.go
321
23
12
13

%