Game Dev Journey Day 5

Around and around we go continuing to work with loops when we break out nobody knows.

While loops happen when a condition is true the code will be continually ran. Often they are placed within a coroutine. It is best to leave them within the coroutine to allow the program to breathe as they are constantly using memory, but having the ability to add a WaitForSeconds allows the program to clear enough memory where it is able to process what is happening within it.

It is best to avoid putting the while loops like this. Because if you do there runs the very strong possibility of crashing the program. There is nowhere for the program to escape, because this condition will always be true! Because you will have to go to the task manager and forcibly close the program and you will lose any unsaved work you might have done.

DO NOT DO!

Now if you add a break out condition to the while loop then it would be okay. See the example code below. Where there is a condition that while apples is less than 50 then it is okay to print out my name, but when apples is counted by incrementing apples then break out of the loop.

Following this I went on and did some loops challenges combining the loop information I have gathered so far. The challenge was to print 0–10, then to print even number 11–20, and odd numbers 21–30.

The next challenge was to create a program where you increment the value of a speed by 5 every second. When speed is greater than the max speed, I am to stop incrementing between a random number between 60–120.

Having finished up the loop section I can see how useful they can be and why I would want to use them in my programming.

Unity Developer