Road to Go Pro — Flow control & Loop
A few words before we start. You can find the code used in this tutorial in this repository. You can find the full contents of Road to Go Pro here. If you missed the last one, you can find it via this link.
We talked about the basic types and data structures in the last part of the tutorial. In this one, we will talk about flow control, loops and thefmt
package.
Flow Control
if
Firstly, let’s look at the if
statement. One of the most basic flow controls is if and else statements. They control the program to execute different code blocks based on conditions. The syntax of if
statement looks like this.
When conditionA in the if statement returns true, then the code inside the if
brackets will execute. Otherwise, the conditions below will be evaluated in sequence until reaches the last else
code block. Go evaluates conditionB next and if conditionB returns true, the code inside will execute. If conditionB returns false, then the last code block will execute. In a combination of if
, else if
and else
statements…