C++ L04 – Control structures

On this post we are going to start developing our first game. The game we will be writing is going to be an adventure with ASCII graphics, meaning that we will present our world using console output. On the posts that will follow we will expand on this game, divert it to something different and maybe even start using real graphics.

Our goal for this post, is to print a basic map and move our character using console input in the form of north-south-east-west [nsew].


#define is another pre-processor directive. Like we saw before, pre-processor is a program that runs before the compiler and performs text related tasks. #define defines text replaces.


Every occurrence of SEARCH will be replaced with REPLACE

#define MAX 5

int main(){
    int x = MAX;
    return 0;

On the example above, the compiler ‘sees’:

#define MAX 5

int main(){
    int x = 5;
    return 0;

#defines have a lot of disadvantages but are very easy to use. For small programs they cannot inflict a lot of harm. However, the dumb and simple nature of the pre-processor can lead to some odd error messages, since no type or other checking is performed during that step. There are better alternatives to #defines that we will be seeing later on.


When running a program, we need to able to run code only under certain conditions. This is achieved using the if control structure:



else {

else if(different_boolean_expression)
else if(different2_boolean_expression)
else {

If boolean_expression evaluates to true, then the code will run, otherwise the code will be skipped.

There are two versions, the one uses a code block ({}) and the other a single statement. Code blocks are very similar to statements, with the difference that they don’t return anything. Ifs also have else sections that run when the condition expression is evaluated to false. Else section is optional and can be omitted.

if(…){} is also a statement. That way you can chain else blocks in order to create an else if structure, that multiple conditions are checked back-to-back. We could argue that all versions are special cases of a general rule.

With some thought, it is easy to come to the conclusion, that if we reach to check a condition, all the previous conditions in the if,else if block have to be false.

The following code checks if the variable x is in (-infinity, -100],(-100,0),[0,infinity) sets and prints an appropriate message

if(x <= -100){
    std::cout << "x in (-infinity, -100]"
else if(x < 0)
   std::cout << "x in (-100, 0)";
   std::cout << "x in [0, +infinity)";

Notice that, we do not have to check for both the edges of the sets, since the one of the edges was checked on the previous else if section.


While is like if, but it runs on a loop. While the condition is true, the block continues to run.


Of course, expression can be a single statement, a code block, can be an if, or even another while!

A common use for while, is to iterate over a number sequence like i=0,1,2,3,4,5,6,…,M-1. Code for such iteration given below

int i = 0;
while(i < M)
    std::cout << i++;

In C and C++ it is standard practice to start counting from 0 and check with less operator. It will be much clearer why later on, when we take a look on pointers.


If we wanted to print the numbers i=1,…M instead of i=0,..M-1, what changes we should do on the previous snippet of code?


The previous while structure is so common, that there is a loop structure mostly for that! Meet for:

for(initial_expression; check_expression; at_end_of_iteration)

for(int i = 0;i < M;++i){
    std::cout << i << "\n";

At the start of the loop, the initial expression is evaluated. Then, before entering the loop the check_expression is evaluated and if it true, then the loop continues, else it stops. At the end of every iteration and before the check, the third part of for is evaluated.


rewrite the previous for, using while

do … while

Another loop structure is do .. while. I personally don’t use it very much, but here you go:

do {
} while(boolean_expression);

Block of code is run at least once. Then the boolean_expression is evaluated. If it is true, it continues. Execution of the loop is stopped otherwise.

What you have to keep in mind is that all loop structures can be rewritten to an equivalent one, using a different loop structure.

break, continue

You can ‘escape’ from a loop early using theĀ break; statement. You can also skip an iteration and go to the next using the continue; statement.

int x = 0;
    if(x++ > 10)
    std::cout << x << "\n";

for(int i=0; i < 20; ++i){
    if(i == 10)
    std::cout << i << "\n";

Reading from console

You should have learnt by now that console output uses std::cout and operator <<. Like cout, cin supports multiple types. Just keep in mind, that getting types mixed up (like providing character input when expecting number) can lead to the program breaking at the point of no return.

std::cin >> variable_to_be_read_into;

Game loops

A very important concept of video games and a lot of other programs, is that of the game loop. During every iteration of the game loop, a frame is simulated and drawn on display. The game loop keeps running until we exit the game.


At check_input phase, we process inputs from the player. Then we simulate the “world” of our game. At the end, we update the display. Of course, there are more sophisticated versions of this loop, but the idea remains mostly the same.


An useful operator is modulo. It is the remainder of the integer division between two numbers and it is the operator %. For example:

std::cout << (10 % 3);//1
std::cout << (11 % 3);//2
std::cout << (12 % 3);//0

As you can see it loops around. In order to produce a sequence of numbers like this: 0,1,2,...,N,0,1,2,...,N,...
You can write code like this:

for(int i=0; i<100000; ++i){
    std::cout << (i % (N+1));

The first version of the game

Now that we have covered all the base knowledge required, we can move on to the actual game code

#include <iostream>

#define MAP_SIZE_X 5
#define MAP_SIZE_Y 4
#define DOOR_TILE_SYMBOL '$'

int main()
    int character_x = 2;
    int character_y = 2;
    char character_symbol;

    int exit_x = 3;
    int exit_y = 1;

    std::cout << "Welcome to the dungeon\n";
    std::cout << "Choose your character:";
    std::cin >> character_symbol;

    bool done = false;
        //print map
        for(int j=0;j<MAP_SIZE_Y;++j){
            for(int i=0;i<MAP_SIZE_X;++i){
                if(character_x == i && character_y == j)
                    std::cout << character_symbol;
                else if(exit_x == i && exit_y == j)
                    std::cout << DOOR_TILE_SYMBOL;
                    std::cout << EMPTY_TILE_SYMBOL;
            std::cout << "\n";

        char dir;
        std::cout << "Choose direction [nsew]:";
        std::cin >> dir;
        if(dir == 'n'){
            if(character_y < 0)
                character_y = 0;
        else if(dir == 's'){
            character_y = (character_y + 1) % MAP_SIZE_Y;
        else if(dir == 'w'){
            if(character_x < 0)
                character_x = 0;
        else if(dir == 'e'){
            character_x = (character_x + 1) % MAP_SIZE_Y;
            std::cout << "Invalid direction\n";

        if(character_x == exit_x && character_y == exit_y)
            done = true;
    std::cout << "You exited the dungeon\n";
    return 0;


what parts of the code above correspond to the phases of a game loop?


try giving "ne" as a single input. What happens and why.


try editing the file to make the map bigger, change symbols and make the game run for maximum of 5 frames; after that it should stop the loop.

Author: Lefteris Chatzipetrou

Lefteris is a Co-Founder and CTO over at HAM Systems. He has wide experience in electrical engineering including electronics and embedded systems, mobile, web services and video games development.

Leave a Reply

Your email address will not be published. Required fields are marked *