Control Flow

While Loop in Go

The while loop executes a block of code while a boolean expression evaluates to true. It terminates as soon as the expression evaluates to false. The boolean expression is evaluated before each iteration.

Instead of using the 'while' keyword, it uses the 'for' keyword with only a Boolean expression.

There is no do-while loop in Go.


Syntax
for booleanExpression {
    //do something
}

Notes

Because the boolean expression is evaluated before each iteration, the block of code executes ONLY if the booleanExpression evaluates to true.

booleanExpression results in either a true or false output. It is created using comparing operators (==, >, =, statement.


Example
x := 0 
for x < 10 { 
    fmt.Println(x) //prints x until x < 10 evaluates to false
    x++ 
}

< For Each   |   Function Declaration >

© 2019 SyntaxDB. All Rights Reserved.