Golang Basic 6 - Basic for loop

Golang Basic 6 - Basic for loop

for loop


  • Go has only one looping construct, the for loop.

  • The basic for loop has three components separated by semicolons:

    1. the init statement: executed before the first iteration.
    2. the condition expression: evaluated before every iteration.
    3. the post statement: executed at the end of every iteration.

    </li>
  • The init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.

  • The loop will stop iterating once the boolean condition evaluates to false.

  • Note: Unlike other languages like C, Java, or JavaScript there are no parentheses surrounding the three components of the for statement and the braces { } are always required.

  • </ul>

    Basic usage 1

    Statement

    for boolVal{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
    }
    

    Sample

    root@go:l6/ # cat l6s1.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i <= 3{
            fmt.Println(i)
            i
    
        }
    }
    root@go:l6/ # go run l6s1.go
    0
    1
    2
    3
    

    Basic usage 2

    Statement

    for initial; boolVal; repeat{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
    }
    

    Sample

    root@go:l6/ # cat l6s2.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i = 1; i <= 3; i   {
            fmt.Println(i)
    
        }
    
    }
    root@go:l6/ # go run l6s2.go
    1
    2
    3
    

    Break

    • You can use break statement to break current loop.

    Statement

    for boolVal{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
        if flag {
            // if flag is true, break the for loop.
            break
        }
    }
    

    Sample

    root@go:l6/ # cat l6s3.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i = 1; i <= 3; i  {
            fmt.Println(i)
            if i == 2{
                break
            }
    
        }
    
    }
    root@go:l6/ # go run l6s3.go
    1
    2
    

    Continue

    • You can use continue, to pass current loop, continue running next loop.

    Statement

    for boolVal{
        /*
            if boolVal is true, run this code block.
            will stop while boolVal is false.
        */
    
        if flag {
            // if flag is true, break the for loop.
            continue
        }
    
        /*
            if the flag is effact, you can not see the result of statements here.
        */
    }
    

    Sample

    root@go:l6/ # cat l6s4.go
    package main
    import (
        "fmt"
    )
    
    func main(){
        var i int
        for i = 1; i <= 3; i  {
            if i == 2{
                continue
    
            }
    
            fmt.Println(i)
    
        }
    
    }
    root@go:l6/ # go run l6s4.go
    1
    3