Control Flow

For Loop in Go

The for loop is a control structure that executes a block of code a specified number of times.


Syntax
for initialization; booleanExpression; update {
    //do something
}

Notes

Initialization declares the initial value of the controlling variable. The controlling variable can be defined in-line with the for loop, or defined previously. Typically, the for loop uses an integer variable.

booleanExpression is the controlling statement that must be true in order for the 'for' structure to loop and execute. booleanExpression must result in either a true or false output. It is created using comparing operators (==, >, =, <=, !=). ; There can also be multiple boolean expressions within the parentheses (booleanExpression). The boolean expressions are connected through logical operators (&&, ||, !).

Update is the change in the controlling variable that occurs after each execution of the statements. It performs an arithmetic operation on the controlling variable.


Example
for x := 0; x <10; x++ { 
    fmt.Println(x) 
} //x will continue to increment while it remains less than 10.

< Switch Case   |   For Each >

© 2018 SyntaxDB. All Rights Reserved.