Game Dev Journey Day 4

Chris Didier
3 min readMar 25, 2021

Today is working with loops. Loops are allow you to save a lot of time by having the code do a the tasks required the amount of times you would like instead of writing the same amount of code a million times over.

In this example you can see that the name is printed 100 times by using the following code.

This can be great for getting the correct amount of iterations of a cycle and you can even skip cycles you want data collected from. Here is an example of collecting only even numbers from 0- 100.

If you wanted the odd numbers just changed it to.

One important part is having the code break out of the loop. If you don’t break out of the loop you could potentially be stuck in it forever and never get to the rest of your code in the program.

Breaks out of loop at 50 instead of going to 100

Using a for loop within a coroutine also gives us the option to watch the counter 1 second at a time. In this example we have 100 apples and the counter counts 1 second at a time till 100 apples are counted.

For loops are helpful tools that can be used in all forms of programming. That will be building blocks in many programs going forward.

--

--