Programming & Scripting Tutorials

C++: If and Else Statements



So we’ve learnt how to collect basic data from the user, but wouldn’t it be useful if we could do different things depending on what the user typed in?
Well we can do exactly that with if statements. These are basic statements, which allow us to compare two values (be them constant or variable).

The first thing we’re going to learn about, is the ‘if’ itself. Just write the keyword ‘if’ and then in some brackets, the condition. To specify the condition you simply write one value (either a variable or constant), then the comparison operator you want to compare them with (for example – equal to, which is ‘==’) and then the second value (either a variable or constant). We then put some curly brackets, and anything inside the curly brackets is what will be executed if the condition is true. For example, the following would compare 1 to 1 (which is obviously always true):

if(1==1)
{
	//Stuff to do if the condition is true
}


Note that the value that you are comparing the first thing to, must match the type of the first thing – for example, if comparing a string you must either compare to a string variable, or to a string constant (and remember that string constants are always shown in double quotes).
Also note than in our example, 1 always equals one so it’s not much of a condition - we can obviously use variables to actually create a useful condition. For example:

int age;
cin >> age;
if(age==16)
{
	cout << "Wow, I’m 16 too!";
}


In this case the program would output "Wow, I’m 16 too!" if the user entered 16. We can also compare two variables using the same method:

int age, height;
cin >> age;
cin >> height;
if(age==height)
{
	cout << "Your age and height are the same!";
}


The issue we have at the moment, is that we aren’t always going to want to just check if something is equal to something else. What if we wanted to check if something was less that, or greater than, or not equal to? Well luckily – there are other comparison operators we can use instead of just being restricted to the equal to operator (‘==’).
Less Than (<) and Greater Than (>) are relatively simple – they are simply their symbols. So we could check if the users height is greater than their age like this:

int age, height;
cin >> age;
cin >> height;
if(height>age)
{
	cout << "Your height is greater than your age!";
}


We can also do "Greater Than or Equal To" and "Less Than or Equal To" by simply adding a single equals sign after the appropriate symbol. For example, we could check if the users height was less than or equal to their age like this:

int age, height;
cin >> age;
cin >> height;
if(height<=age)
{
	cout << "Your height is less than or equal to your age!";
}


There are also the simple equal to and not equal to operators. We already know equal to as ‘==’, and not equal to is an exclamation mark followed by an equals sign (‘!="). So we could check if the users height doesn’t equal their age like so:

int age, height;
cin >> age;
cin >> height;
if(height!=age)
{
	cout << "Your height doesn’t equal your age!";
}


Ok, so now that we can compare two values pretty well – what if we want to have multiple conditions. For example if we wanted to do one thing if their height and age were equal, and if they aren’t then do something else if another condition is met..
Well we can accomplish this using ‘else if’. You can just write ‘else if’ after your closing curly bracket for your original if statement, and then specify your ‘else if’ conditions followed by the curly brackets to contain the code to be executed if they are met. For example:

int age, height;
cin >> age;
cin >> height;
if(height==age)
{
	cout << "Your height is equal to your age!";
}
else if(height<age)
{
	cout << "Your height is less than your age!";
}
else if(height>age)
{
	cout << "Your height is greater than your age!";

}


Notice that it’s very easy just to string together ifs and else-ifs, in this case I’ve just put an if and two else-ifs. We can also specify something to do if none of the conditions are met (in our example above this wouldn’t really be useful since it’s impossible not to meet any of the conditions – but it’s generally good practice to put an else in just in case something goes seriously wrong). We can do this using the ‘else’ keyword, and then some curly brackets to specify the code that could be executed at the end of our "daisy chain" of ifs and else-ifs:

int age, height;
cin >> age;
cin >> height;
if(height==age)
{
	cout << "Your height is equal to your age!";
}
else if(height<age)
{
	cout << "Your height is less than your age!";
}
else if(height>age)
{
	cout << "Your height is greater than your age!";

}
else
{
	cout << "Something has gone seriously wrong here!";
}


A great example of "daisy chaining" these all up is to create a program which asks for a student’s test score, and then spits out the grade that they got. Before I give you the code for the example I made – I just want to quickly tell you about pausing the program in a different way. We have previously used system.pause and cin.ignore – but neither are very good solutions. The best solution for the time being, is probably to use getch(); – it simply get’s a character, and in the case of simply calling it as we will, ignore it. Getch does however require another include – conio.h. This can be seen in use in the example below:

#include <iostream>
#include <conio.h>

using namespace std;
int main(){
    int mark;
    cout << "What mark did they get in the test?" << endl;
    cin >> mark;

    if(mark >= 90)
    {
            cout << "You got an A*" << endl;
            cout << "You Passed!" << endl;

    }
    else if(mark >= 80)
    {
            cout << "You got an A" << endl;
            cout << "You Passed!" << endl;

    }
    else if(mark >= 70)
    {
            cout << "You got an B" << endl;
            cout << "You Passed!" << endl;

    }
    else if(mark >= 60)
    {
            cout << "You got an C" << endl;
            cout << "You Passed!" << endl;

    }
    else if(mark >= 50)
    {
            cout << "You got an D" << endl;
            cout << "You Failed!" << endl;

    }
    else if(mark >= 40)
    {
            cout << "You got an E" << endl;
            cout << "You Failed!" << endl;

    }
    else
    {
            cout << "You got an U" << endl;
            cout << "You Failed!" << endl;

    }
    getch();
    return 0;
}

This C++ tutorial was written by


Back to C++

Advertisement: