Loops might sound a bit odd at first? They are a way of executing parts of our C++ code more than once, looping over code until we have the result we want or a test is done. This can simply mean doing the same thing until the code being looped over prompts the loop to end or it could be a predetermined number of times as specified by the C++ loop itself. We won’t be learning all the types of loop that the language has to offer but just enough to advance our game programming.
[widgets_on_pages id=”udemy_advert_cpp_1″][widgets_on_pages id=”udemy_code_details”]

C++ while loops

The  while loop is nice and straightforward. Think back to the if statements and their conditions. We can use the same combination of operators and variables in the conditional expression of the  while loop. With the while loop we also use an expression that can evaluate to true or false. As with if statements, if the expression is true the code executes. The difference with 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.

int numberOfEnemies = 100;

while(numberOfEnemies > 0)
{

	numberOfEnemies--;
	// numberOfEnemies decreases by one each pass through the loop

}
// numberOfEnemies is no longer greater than 0

This is what is happening. Outside of the while loop int numberOfEnemies is declared and initialized to 100. Then the while loop begins. Its condition is numberOfEnemies > 0. So the while loop will continue looping through the code in its body until the condition evaluates to false. So the code above will execute 100 times.

On the first pass through the loop numberOfEnemies equals 100 then 99 then 98 and so on. But once numberOfEnemies is equal to 0 it is of course no longer greater than 0. Then the code will break out of the while loop and continue to run after the closing curly brace.

[widgets_on_pages id=”bcgp_cfgp_gpp”]

Just like a  if statement, it is possible that the while loop will not execute, even once. Take a look at this.

int availableCoins = 10;

while(availableCoins > 10)
{

	// more code here.
	// but it will never run unless availableCoins is greater than 10

}

Moreover, there is no limit to the complexity of the expression or the amount of code that can go in the loop body.

int playerLives = 3;
int alienShips = 10;

while(playerLives !=0 && alienShips !=0 )
{

	// Entire game code here.
	// ...
	// ...
	// etc.

}
// continue here when either playerLives or alienShips = 0

The above while loop would continue to execute until either playerLives or alienShips was equal to zero. As soon as one of those conditions occurred the expression would evaluate to false and the program would continue to execute from the first line of code after the while loop.

It is worth noting that once the body of the loop has been entered it will always complete at least once, even if the expression evaluates to the value of false part way through as it is not tested again until the code tries to start another pass. For example.

int x = 1;

while(x > 0)
{

	x--;
	// x is now 0 so the condition is false
	// But this line still runs
	// and this one
	// and me!

}

The above loop body will execute once. We can also set up a while loop that will run forever; unsurprisingly called an infinite loop. Here is one.

int y = 0;

while(true)
{

	y++; // Bigger..... Bigger...

}

Breaking out of a while loop

We might use an infinite loop like this so that we can decide when to exit the loop from within its body. We would do this by using the break keyword when we are ready to leave the loop body. Like this.

int z = 0;

while(true)
{

	z++; // Bigger.... Bigger.....

	break; // No you're not haha
	// Code doesn't reach here

}

And you might have been able to guess that we can combine any of the C++ decision-making tools like if, else, and switch with our while loops and the for loop we will learn about as well. As an example.

int x = 0;
int max = 10;

while(true)
{

	x++; // I am going to get mighty big!
	if(x == max){
		break;
	} // No you're not haha.

	// code reaches here only until max = 10

}

It would be simple to go on for a long time demonstrating C++  while loops but at some point, we want to start making games. So here is one last concept concerned with loops.
[widgets_on_pages id=”udemy_advert_cpp_2″][widgets_on_pages id=”udemy_code_details”]

 C++ for loops

The for loop has a slightly more complicated syntax than a  while loop as it takes three parts to set up. Have a look at the code first then we will break it apart.

for(int x = 0; x < 100; x ++)
{

	// Something that needs to happen 100 times goes here

}

Here is what all the parts of the for loop condition do.

for(declaration and initialization; condition; change after each pass through loop)

To clarify further we have:
Declaration and initialization: We create a new int variable i and initialize it to .
Condition: Just like the other loops, it refers to the condition that must be true for the loop to continue.
Change after each pass through the loop: In the example i++ means that 1 is added/incremented to i on each pass.

for(int i = 10; i > 0; i--)
{

	// countdown

}

// blast off i = 0

The for loop takes control of initialization, condition evaluation and the control variable on itself. We will use for loops in a game project quite soon.

We have learned a lot of C++ concepts, keywords, and structures. It is probably easy to imagine how our code could quite quickly become long and sprawling. So let’s look at how we can organize our game using C++ functions.