In programming, you often need to do the same thing more than once. The obvious example that you have seen so far is your game loop. There are a few different types of loops. In this video, learn about the most commonly used.
- [Instructor] Hi, and welcome to section four, loops, arrays, switch, and numerations and functions, implementing game mechanics. In this section, we will start off with loops and arrays. We would then move on to making decisions with switch and class enumerations. At the end, we would learn getting started with functions and growing the branches. Now we move on to the first video of the section that deals with loops. In this video, we are going to take a look at while loops and for loops.
In programming, we often need to do the same thing more than once. The obvious example that we have seen so far is our game loop. With all the code stripped out, our game loop looks like this. There are a few different types of loop, and we will look at the most commonly used. The correct term for this type of loop is a while loop. The while loop is quite straightforward. Think back to the ifs statements that evaluate it to either true or false. We can use the exact same combination of operators and variables in the conditional expression of our while loops.
As with if statements, if the expression is true, the code executes the difference in comparison. A while loop, however, is that the C++ code within it will continue to execute until the condition is false. Take a look at this code. This is what is happening in the previous code. Outside the while loop, int number of zombies is declared and initialized to 100. Then the while loop begins. This conditional expression is number of zombies greater than zero, therefore the while loop will continue looping through the code in this body, until the condition evaluates to false.
This means that the code above will execute 100 times. On the first pass through the loop, number of zombies equals 100, then 99, then 98, and so on. But once the number of zombies equals to zero, it is, of course, no longer greater than zero. The code will break out of the while loop and continue to run after the closing curly brace. Just like an if statement, it is possible that the while loop will not execute even once. Take a look at this.
Moreover, there is no limit to the complexity of the expression, or the amount of code that can go in the loop body. Consider this hypothetical variation of a game loop. This while loop will continue to execute until either player lives, or alien ships was equal to zero. As soon as one of those conditions occured, the expression would evaluate to false, and the program would continue to execute from the first line of code, after the while loop. It's worth noticing that once the body of the loop has been entered, it will always complete at least once, even if the expression evaluates to false partway through, as it is not tested again until the code tries to start another pass.
For example, the previous loop body will execute once. We can also set up a while loop that will run forever, unsurprisingly called an infinite loop. Here's an example. If you find the above loop confusing, just think of it literally. A loop executes when its condition is true. Well, true is always true and will therefore keep executing. We might use an infinite loop so that we can decide when to exit the loop from within its body, rather than in the expression. We would do this by using the break keyword when we are ready to leave the loop body.
Perhaps it would look like this. You might always have been able to guess that we can combine any of the C++ decision making tools, such as if, else, and another, and another we will learn shortly, switch, within our while loops, and other loop types as well. Consider this example. We could go on for a long time looking at the various permutations of C++ while loops. But at some point, we want to get point to making games. Let's move on to another type of loop, that is, for loop.
The for loop has a slightly more complicated syntax than a while loop because it takes three parts to set one up. Have a look at the code first, then we will break it apart. Here's what all the parts of the loop condition do. First, int x = 0, is declaration and initialization part. Then, x less than 100 is the condition. Lastly, x increment is done to change the value before each iteration.
To clarify further, here's a table to explain all the three parts as they appear in the previous for loop example. We can vary for loops to do many more things. Here's another simple example that counts down from 10. The for loop takes control of initialization, condition evaluation, and the control variable upon itself. We will use for loops in our game in later sections. Cool! In this video, we have learned while loops and for loops. Awesome! In the next video, we will take a look at arrays.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Creating the main game loop
- Drawing the game background
- Handling errors
- Manipulating C++ variables
- Randomizing numbers
- Making decisions with if, else, and switch
- Moving game objects
- Pausing and restarting games
- Adding scoring and messages
- Using class enumerations and arrays
- Adding sound effects
- Object-oriented programming in C++
- C++ references
- Texture management
- Collision detection
- Implementing the HUD
- Finishing the game