Golang Operators, Loops, if and else for beginners

Tia Louden
5 min readAug 29, 2018

--

How to be a smooth Operator? Oh, Go on then! (I’m not going to stop, they will get worse).

The last blog went through some of the basic principles of data types, variables and how to use them. This time, I’m going to look into operators as well as if and else statements. They’re each accompanied by examples that you can try and alter to make it do something else fun!

If you want to read my other blogs before diving into this one, here are the links:

Getting started learning Go

Golang for an absolute beginner

Golang variables and data types for beginners

Operators

Operators are symbols that tell the programme to carry out a specific action to produce a final result. They can be arithmetic, logical, or comparisons designed to reduce the amount of typing that the developer needs to do.

The tables below have been taken from the GitHub Cheat Sheet included in the Bibliography and will be kept handy as I head into a later section introducing if and else statements.

This one will be with me for quite some time as I get used to coding some more slightly complex programmes. But even building the smaller ones that I’ve shown below, watching the output generate in the terminal has been so much fun.

Loops

Why do we use loops? Go has one form of loop called the ‘for loop’ which repeats the code several times over until it’s told to stop so that the developer doesn’t need to replicate work. To demonstrate the usefulness of loops I built a timer counting down from 10–1 repeating an fmt.println each time, as you can see below. Now imagine having to do that for 100 or 1000, it’s just not practical.

Building a for loop allows you to write less code and it introduces some of the Operators that I earlier, as well as a new syntax ‘:=’, which indicates that a new variable is being created and set.

This time, I’ve moved the comments in the code to where they would if this was production code.

And this is how the output looks for both:

I can’t show you without filming how it adds a second before printing each value so you should try it yourself to watch it happen!

You can see why it wouldn’t work if you counted down from 100 or any other large number, it’s inconceivable to type all that out! Whereas, with the loop I can change line 8 i := 100 or whatever number I need, give it a go if you like!

If and Else ‘Conditional’ statements

Here, I’m going to introduce the principles of If and Else. If else conditionals provide a set of options and determine an outcome based on the information provided. The programme checks the conditions and makes a decision on which condition matches and what action to take. For example, if it’s raining I’ll take an umbrella when I leave the house, else if it’s not raining, I won’t.

Below is a very basic if else programme which is asking whether the number 17 is odd or even. We’ve told the programme that the variable = 17 because Go automatically assigns a variable value of zero if you don’t provide it with one.

The programme is using the % operator listed in the arithmetic table above and this operator asks what the remainder is when the variable is divided by 2. It leaves a remainder of one (odd number) or zero (even number) as you can see below.

Just to prove the code works, here is the output for numbers 10 and 17:

You need to be careful with if else statements, the braces {} are important otherwise your code will not work. Miss one off and you will get an error because the compiler won’t know what you mean. Imagine that you’re trying to read a paragraph of text with no punctuation whatsoever, it’s hard, right? That’s the equivalent of what you would be forcing the machine to try to do.

Now I’ll try something a little more complicated with if else statements and increase the output possibilities.

And this is the output:

To get the results you can see above I started with 53 on line 7 in the code and decreased the age by 10 each time to confirm that I got the output I wanted. Regardless of what number I input over 50 the output will be the same, whether that’s 63 or 103.

Over the next few weeks, I’m going to come up with some more examples of if else conditionals and loops that become increasingly more complex to cement this theory properly.

Review

The next blog is going to be on Slices, which I’m currently learning. So, while I do that for the next few weeks while I go through the next set of lectures on slices I’m going to test myself a little more and start to make the programmes that I wrote above work in real life situations, or introduce floats rather than just integers and generate a string.

As ever, if you have any questions, complaints about puns or any notes on the blog feel free to tweet me or leave comments. Thank you all very much for the continued support!

--

--

Tia Louden
Tia Louden

Written by Tia Louden

Woman in Tech, sharing tech stuff, and other learnings.

No responses yet