LoginSignup
0
0

More than 5 years have passed since last update.

[メモ]goのfor

Posted at
package main

import "fmt"

func main() {
    // rangeを回すループ
    hoge := []int{1,2,3}

    for _, v := range hoge {
        fmt.Printf("%v", v)
    }


    // 古典的ループ
    for i := 0; i < 10; i++ {
        if (i % 2 ==1) {
            continue
        }
        fmt.Printf("%v", i) 
    }

    // 条件式ループ
    count := 1
    for count < 100 {
        fmt.Printf("%v", count)
        count++
    }

    // 無限ループ
    for {
        fmt.Print("infinite loop")
        break       
    }
}

こんな感じの条件式に構造体の状態を用いたループもできる。
Go言語で作るインタプリタより抜粋


    for !p.curTokenIs(token.RBRACE) && !p.curTokenIs(token.EOF) {
        stmt := p.parseStatement()
        if stmt != nil {
            block.Statements = append(block.Statements, stmt)
        }
        p.nextToken()
    }

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