Conditionals and Loops
#
If StatementsIf statements in Go do not require any parentheses.
If can have a short statement before the condition:
Note that, in the above example, x
is only available for use in the lexical scope of the if
and else
statements. Hence, it cannot be referenced anywhere outside this scope.
#
Switch StatementsWhen we have a sequence of conditions to check, it will be easier to use the switch
statement.
We can also construct a switch
statement without a condition. This is the same as saying `switch true.
Switch statements by default have an implicit break
without them being explicitly included. This means that once a case matches the condition, it evaluates it and stops there.
#
Flow ControlGo only has one looping construct - the for
loop.
There are three ways to use the for
loop in Go. You can make out similarities to while
loops as in other languages if you are used to them.
Most importantly, the loops are pretty logical and its very "readable".
Notes: {}
are always required. No parentheses, ()
, needed for the three components in the for loop declaration.