Learn to Code via Tutorials on Repl.it!

← Back to all posts
Golang Googles C++ Part 5: For Loops
JustAWalrus (1187)

Sorry for the long wait, I just don’t have any motivation to do these anymore.

Anyway today we will talk about loops!

What the heck is a loop!?!?!?!!

Umm, well a loop is a snippet of code that keeps on running while a certain condition is true.

Well, what are the kinds of loops?

There is only 1 type in go: a for loop!

Basically, a for loop is a loop you might be used to is a loop that iterates over for something in something else.

But in Go they are different.

In Go a for loop is basically a combo of the loop your used to and a while loop.

So how do they work?

Well let’s look at a basic for loop:

for i := 0; i < 10; i++ {
    sum += i

What does this all mean?!
Well we can break it down:

The for is a keyword telling Go that we are making a For loop.

The i := 0 is code that will run before the loop starts. This can be anything.

Then we have a semicolon.

Then i < 10 this is the condition that as long as it is true the loop will keep running.

Another semicolon.

Then i++ This is code that will be repeated every time the code in the loop runs.

Then the brackets you should recognize these from last tutorial.

Anyway that’s it. Again, sorry for the long wait I just haven’t had any motivation lately.

Please upvote for more!