Chapter 3. Control Flow
In the previous chapter, we covered the basics of GO data types and, in a few examples, used flow control to print messages.
In this chapter, we’ll use what we learned about comparison operators and boolean values to learn more about flow control.
Elements of Flow control
Programs are like water in a river because they tend to flow in one direction. And like water in a river, parts of it can go off into separate outlets based upon the river’s conditions, such as a boulder in the stream or a dam.
However, unlike water in a river, it’s much easier to divert the flow of a program.
To do so, we use conditions to evaluate a variable’s value or the output of a function and execute a different code block.
Let’s use an example with comments to see.
We are using the familiar ‘if’ from the previous chapter.
In this program, we’re checking whether a ‘random’ integer number is even or not via the modulo operator %
, which is a fancy way of saying ‘dividing with remainders’.
If you recall, an even number can be divided in two without any remainders, whereas an odd number cannot.
Don’t worry about the math. Just note that the logic of the program changes based on the condition, which is the ‘random’ value of the n variable.
Now, let’s introduce the rest of the gang for flow control.
Flow Control Statements
‘if’ statements
These are the most used control statements, and you’ll almost always use them when adding ’logic’ to your programs.
The general pattern is that your program has a chunk of code that will only be executed if the condition is considered ’true’ or ‘false.’
’else’ statements
Technically, in the previous ‘if’ example, we could’ve had an ’else’ statement to print the ’number is odd:’ part, but we kept it cleaner by leaving it out, which is a preference amongst programmers.
Else statements say ‘hey, if this piece of logic turns out to be false or something else, then do this.’
Typically, ’else’ statements aren’t preferred because they aren’t explicit and will catch only when the condition is false.
Let’s introduce the else’s, cousin.
else if
Here, we can communicate what we want to happen ‘if’ the first condition evaluates to ‘false’ and explicitly define what should happen.
switch
The ‘switch’ control flow works much the same as using ‘if’ ’else’ or ’else if’ but presents differently in the form of ‘cases’. A ‘case’ is the same as a condition and may be preferred by those with a mathematical background. Either style works; use whichever makes sense to you.
In the above, we say ‘switch’ based upon the following ‘case’ and pass in some data. The same again can be accomplished by using ‘if’ and ’else if’
In general, I prefer using ‘switch’ and including the optional ‘default’ case because I find it easier to test my ‘cases’ and to convey to others what I’m trying to do with my code.
Loops
Now that we have some logic to control a program’s flow, it’s time to introduce ’loops’. A loop is a way we tell computers to “do work for x amount of times”. Let’s combine it with loops to make a more intelligent program.
A ‘for’ loop also uses a condition to do its work, but it works ‘until’ that condition is satisfied. So, for example, the saying ‘a thousand thanks’ we’ll have a program that will say ’thank you’ a thousand times based upon a reader’s name.
In the above the ‘for’ loop syntax is for initial condition; compare ;if not met then do another { // work to be done }
In most programming languages, it is common to start at ‘0’, so keep this in mind when working with arrays, slices, and initial loop conditions.
Okay, let’s have some fun by building a few small games to show your friends and family at the winter holiday parties.
Don’t worry if some syntax is unfamiliar. We want to focus on building muscle memory and familiarity with GO. So, try your best to copy the code and get in running. If you have any troubles at all the source code is available
Guess the Number
In this first game we’ll have the computer pick a random number and then try to guess it by inputting guesses, but the catch is we only get ‘3’ attempts!
But, we’ll give the user clues to increase the chances of winning within 3.
Advanced: Rock, Paper, Scissors
Now, for a more fun and more advanced example to wet your programmer appetite.