Bold Aesthetic Creative Games

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

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

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

In the previous post, we learned some conditional statements. Now, we are going to learn more about them. And later on, we will learn even more…

But for now, just check an example of double if-statements again:

#include <iostream>
using namespace std;

int main()
{
int a = 3;

if(a < 10)
    cout << "a is smaller" << endl;
a = 15;
if(a > 10)
    cout << "a is bigger" << endl;
a = 5;
cout << a << endl;

return 0;
}


Everything looks fine in the above code and also compiles fine. It runs fine as well. But the problem is that I don’t intend to do that! I don’t want this output. I want it to do something like this:

If a is smaller than 10, print “a is smaller” and set a equals to 15 else if a is bigger than 10, print “a is bigger” and set a equals to 5. At last, print a which should be equal to 15 or 5.

The output of the above code is:

a is smaller
a is bigger
5

What I think or intend is:

a is smaller
15

Our goal is to understand how to get the output that we intended even for such a simple case. You may have figured out some mistakes. Following are some of them:

  • There are two lines which I intend to execute when respective condition passes. So, we must use block(curly brackets) for both if-statements.
  • I want to use else-statement.

Now, after the correction applied, it may look like:

if(a < 10)
{
    cout << "a is smaller" << endl;
    a = 15;
}
else
{
    cout << "a is bigger" << endl;
    a = 5;
}
cout << a << endl;


Yeah! We fixed it! But hey, we removed the condition entirely! Where is a > 10? This is another mistake which can be solved like this:

if(a < 10)
{
    cout << "a is smaller" << endl;
    a = 15;
}
else
{
    if(a > 10)
    {
        cout << "a is bigger" << endl;
        a = 5;
    }
}
cout << a << endl;


The question is that, why we need to have that condition? Well, what if a is equal to 10? If it is, it should not pass both of the conditions. But if we use else only, this is no more true. Therefore, it is necessary to have if statement in else statement’s block with the appropriate condition.

From this question, we get another one which is more like a suggestion. What if we want to output something for each and every case? If a is smaller than 10 then do something else if a is bigger than 10, do something, else do something. Of course, the second and last else-statement is only for the value of a equal to 10.

Here’s the code: (Remember, else-statement can only be put with an if-statement, not another else-statement)

if(a < 10)
{
    cout << "a is smaller" << endl;
    a = 15;
}
else
{
    if(a > 10)
    {
        cout << "a is bigger" << endl;
        a = 5;
    }
    else
        cout << "a is ten" << endl;
}
cout << a << endl;


You may have noticed that with the other else-statement, the block is not used as the code is a single line. It is quite confusing and doesn’t look easy. Well, here’s an easy way. We can use else-if-statement.

if(a < 10)
{
    cout << "a is smaller" << endl;
    a = 15;
}
else if(a > 10)
{
    cout << "a is bigger" << endl;
    a = 5;
}
else
    cout << "a is ten" << endl;

cout << a << endl;


Both of the codes are equivalent. Both give the same output for all cases. But with else-if-statement, it’s better. You can use else and else-if-statement, after an else-if-statement.

You see! It is slowly getting complex. But don’t worry! If you read this and then, try to code things right, you are doing all well!

Leave a Reply

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