Tutorial 4 – Control Flow Statements

In the last tutorial we touched briefly on some thing called a for loop. In this tutorial we’ll be looking a bit more closely at that and at a few similar statements that we can make in C++.

So we know that a for loop repeats code a certain number of times. Lets take a closer look at it’s syntax:

for(int i = 2; i > -5; i--)
{
    //Code goes here
}

When this statement is reached the index “i” is set to 2. The boolean condition is then evaluated. If this condition is met then the code in the brackets is executed and any changes to the index are made (in this case we decrement it’s value by 1). The boolean check, code execution, and index stages are then repeated until the boolean condition is false.

Next we’ll look at is the “If/if-else” statement. This is a fairly simple statement which allows you to execute different code depending on a given condition. For example:

//If statement
if(true)
{
    //Because the "if" statement is true, any code here will be executed
}

//if-else
if(3 <= 2)
{
    printf("Maths has gone mad!\n");
}
else
{
    printf("Maths is working ok!\n")
}

//if/else-if
if(x < 1)
{
    //Small value
}
else if(x < 2)
{
    medium value
}
else
{
    //must be a large value!
}

As you can see, the if statement allows you to enter different sections of code depending on the outcome of a boolean condition. It is also quite apparent that if-else statements can become quite verbose quite quickly. One way around this is to use the switch statement.

switch(value)
{
    case 5:
        printf("Value was 5");
      break;
    case 4:
        printf("Value was 4");
      break;
    case 3:
    case 2:
    case 1:
        printf("Value was either 3, 2 or 1");
      break;
    default:
        printf("Not sure what the value was");
      break;
}

As we saw with the for loop it is possible to repeat a section of code a specific number of times. But what if we want to repeat some code indefinitely? That’s where the while loop comes in!

while(true)
{
    printf("This will go on forever!\n");
}

This code will do exactly what it says on the tin. While the boolean condition equates to true, perform the code within the loop and then start again.

In some cases we would like to ensure that the code within the loop is performed at least once and then possibly looped. We can use the do while loop for this. The do while works in exactly the same way as the while loop with the exception that the code is performed first and then the boolean statement is checked.

do
{
    printf("I'll say this only once!\n");
}while(false)
  1. No comments yet.
  1. No trackbacks yet.

Tell me what you think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: