Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Using Conditional Statements (Part 3)

C++ Programming Series: Using Conditional Statements (Part 3)

In the last two previous posts, you got through the if, else and else-if-statements. The uncertain point is the difference between two if-statements and if-statement with an else-if-statement.

Two if-statements are not connected to each other. They don’t have any relation with each other. The compiler has to pass through all of these statements even if any one of the conditions is true. The example shows three if-statements:

int value = 6;
if(value > 7)
    cout << "value is bigger than 7" << endl;
if(value > 5)
    cout << "value is bigger than 5" << endl;
if(value > 3)
    cout << "value is bigger than 3" << endl;


Output:

value is bigger than 5
value is bigger than 3

An if-statement with an else-if statement are connected to each other. If anyone of the conditions gets true, all others are skipped. The same example below with some corrections:

int value = 6;
if(value > 7)
    cout << "value is bigger than 7" << endl;
else if(value > 5)
    cout << "value is bigger than 5" << endl;
else if(value > 3)
    cout << "value is bigger than 3" << endl;


Output: (Now, there are no unnecessary outputs. It is pretty nonsense to say that value is bigger than 3 when we already said that value is bigger than 5)

value is bigger than 5

This is very important when it comes to getting user input and then, manipulating it for some change in the output. For example, there is a character variable or variable of type, char called key which takes the character (or letter) that you press on the keyboard and in return, print something for you. (Remember, put single inverted commas and not double, for a character or their variables only)

if(key == 'a')
    cout << "Alpha!" << endl;
else if(key == 'b')
    cout << "Beta!" << endl;
else if(key == 'c')
    cout << "Charlie!" << endl;
else if(key == 'd')
    cout << "Delta!" << endl;
else
    cout << "Unknown input..." << endl;


For such cases, it is better to use another conditional statement called switch-statement. It is only good for comparison done with equality operator (i.e, ==) and numbers.

switch(key)
{
    case 'a':
        cout << "Alpha!" << endl;
        break;

    case 'b':
        cout << "Beta!" << endl;
        break;

    case 'c':
        cout << "Charlie!" << endl;
        break;

    case 'd':
        cout << "Delta!" << endl;
        break;

    default:
        cout << "Unknown input..." << endl;
        break;
}


This code does the same thing as the above one. You may have figured out some of the things by yourself. But let me explain this.

After switch, we have to type in the variable in the round brackets, unlike if-statement. Then, we need to put a case with the value that is to be compared with the variable. At the end of case-value pair, we need to put a colon. Then, put one too many lines of code before the ending flower bracket. What? Did we put a starting flower(or curly) bracket? No! So, that means… there is some other way to put an ending to it. The break keyword is only for switch and loop statements. It is used to skip all the codes below it and is used to skip the whole loop, not just a block (You will understand this later if not now). The default keyword is another case which acts like the else-statement and works only when all the above cases are false.

Want some shortcut to understanding switch-statement? Here is the format:

switch( [variable] )
{
    case [value]:
        // to be executed when the variable's value is equal to that of the case's value
        break;

    // ...
    // [more cases]
    // ...

    default:
        // to be executed when the variable's value is not equal to any of the above case's value
        break;
}


Pretty long but looks useless! Nah! These are the basis of all the beautiful things that you can imagine to do on the screen…

Leave a Reply

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