Flow control is an essential part of every programming language for controlling the direction of logic and the order of execution. It can be regarded as the “meridians” of a programming language.
The most commonly used flow control constructs in Go are if and for, while switch and goto are extended flow control constructs primarily designed to simplify code and reduce duplication.
if else (Branching Structure)
Basic Syntax for if Conditions
The syntax for if conditional statements in Go is as follows:
| |
When expression 1 evaluates to true, branch 1 is executed. Otherwise, expression 2 is evaluated. If it is satisfied, branch 2 is executed. If neither condition is satisfied, branch 3 is executed. Both else if and else in an if statement are optional and can be included as needed.
Go requires the opening brace { that corresponds to if to appear on the same line as the if expression. Placing { elsewhere will trigger a compilation error. Similarly, { corresponding to else must appear on the same line as else, and else must also appear on the same line as the closing brace of the preceding if or else if.
For example:
| |
Special Syntax for if Conditions
There is also a special syntax for if conditions. You can add an execution statement before the if expression and then evaluate the variable’s value. For example:
| |
Question: What is the difference between the two approaches above?
for (Loop Structure)
All types of loops in Go can be implemented using the for keyword.
The basic syntax of a for loop is as follows:
| |
The loop body continues to execute while the conditional expression evaluates to true. The loop exits automatically when the conditional expression evaluates to false.
| |
The initialization statement of a for loop can be omitted, but the semicolon following it must still be included. For example:
| |
Both the initialization and post statements of a for loop can be omitted. For example:
| |
This syntax is similar to while in other programming languages. A conditional expression is added after while, and the loop continues while the condition is satisfied; otherwise, the loop terminates.
Infinite Loop
| |
A for loop can be forcibly exited using the break, goto, return, or panic statement.
for range (Key-Value Iteration)
In Go, for range can be used to iterate over arrays, slices, strings, maps, and channels. Values returned when iterating with for range follow these rules:
- Arrays, slices, and strings return an index and a value.
- Maps return a key and a value.
- Channels return only the values received from the channel.
switch case
The switch statement provides a convenient way to perform conditional checks against a large number of values.
| |
Go allows each switch to have only one default branch.
A branch can contain multiple values, with multiple case values separated by commas.
| |
Expressions can also be used in branches. In this case, the switch statement does not need to be followed by a variable to evaluate. For example:
| |
The fallthrough syntax executes the case following the matching case. It was designed for compatibility with case behavior in C.
| |
Output:
| |
goto (Jump to a Specified Label)
The goto statement performs an unconditional jump between sections of code using a label. The goto statement can be useful for quickly exiting loops and avoiding repeated exit logic. In Go, the goto statement can simplify the implementation of certain code. For example, when exiting a doubly nested for loop:
| |
Using the goto statement simplifies the code:
| |
break (Exit a Loop)
The break statement can terminate the code block of a for, switch, or select statement.
A label can also be added after a break statement to indicate that the code block associated with that label should be exited. The label must be defined on the corresponding for, switch, or select code block. For example:
| |
continue (Continue to the Next Iteration)
The continue statement terminates the current iteration and begins the next iteration of the loop. It can only be used within a for loop.
When a label is added after a continue statement, the next iteration of the loop associated with that label begins. For example:
| |
Exercises
- Write code to print a 9×9 multiplication table.