The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en

Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."

4.5. for statements

There are three basic forms of loop statements:

     for { code block }
     for ConditionalExpression { code block }
     for InitialStatement; ConditionalExpression; LoopOperationStatement { code block }

Among them, for {code block} will keep looping until the statements in the code block use the break keyword to exit the loop. Using the continue keyword will skip subsequent statements and execute the next loop, for example:

    i: int
    for {
        i++
        if i == 2 {
            continue
        }
        println(i)
        if i == 3 {
            break
        }
    }

The above code will output:

1
3

for ConditionalExpression {code block}, before each time the loop executes code block, it will judge whether ConditionalExpression is true, if so, execute the code block, otherwise exit the loop. Statements within a code block can also use break and continue to exit the loop or skip subsequent statements to execute the next loop:

     i: int
     for i < 3 {
         println(i)
         i++
     }

for InitialStatement; ConditionalExpression; LoopOperationStatement {code block}, it first executes the InitialStatement once, and then checks whether the ConditionalExpression is true before each execution of the code block, and executes it if it is code block, otherwise exit the loop; after each code block is executed, a LoopOperationStatement will be executed. Using the break keyword in a code block will exit the loop directly, and using the continue keyword will skip subsequent statements and execute the next loop (at this time the LoopOperationStatement will still be executed), for example:

    for i := 0; i < 100; i++ {
        if i == 1 {
            continue
        }
        println(i)
        if i == 2 {
            break
        }
    }

The above code will output:

0
2