Chapter 4. Looping

In the previous chapter, we discussed the if statement. The if statement enables you to put a condition on the execution of a block of code.

In this chapter, we will explore loops, which are code structures that enable you to repeat a block of code under certain conditions. We stop repeating that block of code once the condition becomes false.

In this chapter, we will explore the following topics:

  • While loops
  • Do/while loops
  • For loops
  • A simple example of a practical loop within Unreal Engine

The while loop

The while loop is used to run a section of the code repeatedly. This is useful if you have a set of actions that must be done repeatedly to accomplish some goal. For example, the while loop in the following code repeatedly prints the value of the variable x as it is incremented from 1 to 5:

int x = 1;
while( x <= 5 ) // may only enter the body of the while when x<=5
{
  cout << "x is " << x << endl;
  x++;
}
cout << "Finished" << endl;

This is the output of the preceding program:

x is 1
x is 2
x is 3
x is 4
x is 5
Finished

In the first line of code, an integer variable x is created and set to 1. Then, we go the while condition. The while condition says that while x is less than or equal to 5, you must stay in the block of code that follows.

Each iteration of the loop (an iteration means going once around the loop) gets a little more done from the task (of printing the numbers 1 to 5). We program the loop to automatically exit once the task is done (when x <= 5 is no longer true).

Similar to the if statement of the previous chapter, entry into the block below the while loop is only allowed if you meet the condition within the brackets of the while loop (in the preceding example, x <= 5). You can try mentally subbing an if loop in the place of the while loop, as shown in the following code:

int x = 1;
if( x <= 5 ) // you may only enter the block below when x<=5
{
  cout << "x is " << x << endl;
  x++;
}
cout << "End of program" << endl;

The preceding code sample will only print x is 1. So, a while loop is exactly like an if statement, only it has this special property of automatically repeating itself until the condition between the brackets of the while loop becomes false.

Note

I'd like to explain the repetition of the while loop using a video game. If you don't know Valve's Portal, you should play it, if only to understand loops. Check out https://www.youtube.com/watch?v=TluRVBhmf8w for a demo video.

The while loops have a kind of magic portal at the bottom, which cause the loop to repeat. The following screenshot illustrates what I mean:

The while loop

There is a portal at the end of the while loop that takes you back to the beginning

In the preceding screenshot, we loop back from the orange portal (marked O) to the blue portal (marked B). This is our first time of being able to go back in the code. It is like time travel, only for the code. How exciting!

The only way past a while loop block is to not meet the entry condition. In the preceding example, once the value of x becomes 6 (so, x <= 5 becomes false), we will not enter the while loop again. Since the orange portal is inside the loop, we'll be able to get to finished once x becomes 6.

Infinite loops

You can get stuck inside the same loop forever. Consider the modified program in the following block of code (what do you think will be the output?):

int x = 1;
while( x <= 5 ) // may only enter the body of the while when x<=5
{
  cout << "x is " << x << endl;
}
cout << "End of program" << endl;

This is how the output will look:

x is 1
x is 1
x is 1
.
.
.
(repeats forever)

The loop repeats forever because we removed the line of code that changed the value of x. If the value of x stays the same and is not allowed to increase, we will be stuck inside the body of the while loop. This is because the loop's exit condition (the value of x becomes 6) cannot be met if x does not change inside the loop body.

The following exercises will use all the concepts from the previous chapters, such as the += and decrement operations. If you've forgotten something, go back and reread the previous sections.

Exercises

  1. Write a while loop that will print the numbers from 1 to 10.
  2. Write a while loop that will print the numbers from 10 to 1 (backwards).
  3. Write a while loop that will print numbers 2 to 20, incrementing by 2 (for example 2, 4, 6, and 8).
  4. Write a while loop that will print the numbers 1 to 16 and their squares beside them.

The following is an example program output of the exercise 4:

1

1

2

4

3

9

4

16

5

25

Solutions

The code solutions of the preceding exercises are as follows:

  1. The solution of the while loop that prints the numbers from 1 to 10 is as follows:
    int x = 1;
    while( x <= 10 )
    {
      cout << x << endl;
      x++;
    }
  2. The solution of the while loop that prints the numbers from 10 to 1 in backwards is as follows:
    int x = 10; // start x high
    while( x >= 1 ) // go until x becomes 0 or less
    {
      cout << x << endl;
      x--; // take x down by 1
    }
  3. The solution of the while loop that prints the numbers from 2 to 20 incrementing by 2 is as follows:
    int x = 2;
    while( x <= 20 )
    {
      cout << x << endl;
      x+=2; // increase x by 2's
    }
  4. The solution of the while loop that prints the numbers from 1 to 16 with their squares is as follows:
    int x = 1;
    while( x <= 16 )
    {
      cout << x << "   " << x*x << endl; // print x and it's  square
      x++;
    }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.14