Tagconditionals

How to Use Control Flow in Go

Control Flow in Go is an important concept. Control flow allows a program to execute in various different ways. You can force code to branch off, make code that loops back on itself, and match code to different instances. In this post, we will cover the some of the common forms of control flow in Go that we can use.

Using If/Else Statements in Go

if_else_go

The first most common style of control flow in Go is the if and if/else statement. If/else statements allow you to create branching code very easily. To do this, you need to use conditional statements and Boolean values. A Boolean value is a data type that can only be one of two values. In the case of Go, we have the true and the false keywords. Conditional statements in Go are statements that will resolve to Boolean values. Below are some examples of conditional statements in Go as well as some of the operators you can use to modify them.

We don’t have to use raw values in our conditionals, we can use variables as well. Here is the basic structure of an if/else statement in Go. Keep in mind that the else clause is not mandatory. Also, there is no ternary shorthand in Go like there is in JavaScript. 

In the code above, we have three different types of if/else statements. In an if/else statement, the code block under the if only runs if the conditional is true. If the conditional is not true and there isn’t a second conditional, then the else code block will run. In the first example above, the if statement checks to see if 9 is equal to 9 which it is. The first print function will be run and the print line in the else code block will be ignored. In the third example, the two conditionals are false, so the code in the if blocks is ignored. The last else block’s code will be run in this case.

Looping in Go

go_for_loopThe next major control flow operator is the for loop. The for loop allows us to loop through a block of code until the conditional is made false. This is a very good code structure for areas in your code that require multiple iterations. You can also use the break keyword to automatically “break” a for loop without making the conditional false. Let us look at some examples.

As you can see from these examples, for loops are very powerful constructs in Go. You can choose to loop over something as many times as you wish; though infinite loops tend to cause errors in normal programs. When we look at channels later in this tutorial we will look at other styles of for loops.

The Switch Operator in Go

switch_case
The last simple control flow structure in Go is the switch statement. A switch statement in Go is very similar to chaining if/else statements together. The major difference is that the syntax is not as redundant for larger pieces of code with many branches. In a switch statement, a code block will only run if its case is matched with the initial condition statement. Here is a simple example.

With the switch statement above, the value of i is being matched with the case number. In this case, because the variable i is equal to three, case 3 will execute and the rest will be ignored. The default case is a case that will run if none of the other cases are resolved to true. Here is a full program that showcases switch and default.

In this program, we assign a variable to the current day of the week using the time.Now().Weekday() function. We then check to see if that day is equal to Saturday or Sunday in our first case. If it is equal to Saturday or Sunday, then our first case will run. If it is not equal to Saturday or Sunday then our default case will execute. This example showcases the ability to combine cases as well as use other values for matching.

Conclusion

In this post, we looked at the control flow in Go. We used the if/else statement, the for loop, and the switch statement. We also looked at conditions and the different operators that we can use to make conditional statements in Go. We touched on Boolean values as well. In our next post, we will look at Arrays and Slices in Go. 

JavaScript Boolean Operators and Conditionals

In the last post, we took a look at Strings, Nulls, Undefined and NaNs in JavaScript. Today, we will take a look at Booleans, their operators, and their logic. A Boolean is yet another JavaScript data type and they relate back to the other data types that we have covered thus far in this tutorial on JavaScript.

The strict definition of a boolean is a value that has a binary set of values; in this case, the value can be either true or false. Booleans are an extremely important part of the imperative style of programming because they control most of the flow of the program.  Various operators and operands can be used to determine in which way a program should go and booleans are the most common type of data used for this particular process.

Breaking Down Computation

If you know anything about computers then you have probably heard about how every piece of data ultimately breaks down to a series of ones and zeroes; this is what is called binary data. Binary data is also technically boolean because there are only two possible values that a binary piece of data can take. In fact, in older languages like early common lisp, binary data was used to denote booleans rather than explicit keywords like what we have in JavaScript and many of the languages that are in use today.

In JavaScript, our Boolean values are the keywords, true and false. To really talk about Boolean values, we should also talk about conditionals.  Conditionals are logical sentences in the programming that evaluate to either true or false. In JavaScript, conditionals follow a format like the block below.

Notice the use of the parenthesis for the conditions and the curly brackets for the code body, also, in the actual code the “then” is implied.

boolean-falseIn JavaScript, certain values and types are considered “falsy”: 0, “”, NaN, null and undefined are all “falsy” values. This means that if we put one of these values as the condition of a conditional statement we will always get a failing statement. It is as almost as if we were inputting the keyword false as the condition. Out of these values, only the empty string "" and 0 will return true if they are set equal to the keyword false. 0 == false and "" == false both return true as though they were both the false keyword. Try inputting various combinations of these different falsy values in conditionals to see what happens.

A real example would look more like this example.

If you open your JavaScript console and type in the code above, you will get the return value of false. You can also chain these conditionals together endlessly (though there are usually better ways with longer conditionals).

Make sure that you always keep a space between your else and ifs. In this case, this statement will return true.

The Basic Operators of Booleans

When writing the condition part of the conditionals, you can make use of eight different operators: greater than >, greater than or equal to >=, less than <, less than or equal to <=, equal to ==, not equal to !=, and the two special operators, equal in type and value === and not equal in type or value !==. The last two operators === and !== are unique to JavaScript because of the dynamic typing in the language and in most cases you will use these over the normal == and != when writing conditions and conditionals.

decision_makingLet us look back at the second example, more specifically the ("54" === "54") condition. If we were to change one of the "54" from a string to a number, the condition will now fail if we keep using ===, however, if we use (==) then the condition will pass because it is only checking the value and not the type as well.
You may also remove the else portion of a conditional statement if you don’t need it, say you only need a return value if the statement passes. You would just write something like the examples below.

If that condition does not pass then the code inside of the brackets will not be run. If you were to change the condition to a condition that passes such as 9 <= 9 then the "it's true" will be returned in the console, otherwise you will just receive a return value of undefined. Go ahead and try it with various different conditions until you understand how the different operators work.

Overview

In today’s post, we talked about the basic operators of booleans and conditionals. We introduced control flow through the use of if and if…else conditionals. In the next post, we will take a look at variables. As always, if you have any questions please feel free to leave them in the comment section below.