Absolute Beginners C++


Lesson 4 - If statements

4.1 If statement

// ex4-1.cpp - if statement
#include <iostream>
using namespace std;
int main()
{
    cout << "Enter a number less than 10 : ";
    int value;
    cin >> value;
    if (value < 10) cout << "Thank you" << endl;
    return(0);
}
ex4-1
Enter a number less than 10 : 5
Thank you
ex4-1
Enter a number less than 10 : 12

Example ex4-1 introduces a new statement which can be used to make a decision.  The 'if' statement has the syntax: 

if (condition) statement;

Where 'condition' is a test on the contents of one or more variables, and 'statement' is some code that should only be executed in circumstances when the test is passed.  In the example, the program only outputs the string "Thank you" when the variable 'value' contains a number which is less than 10.  In general the condition can be a combination of tests of equality '==' (N.B. two equals signs), less than '<', greater than '>', less than or equals '<=', greater than or equals '>=', or not equal '!='.  Tests can be bracketed with parentheses, and combined with the 'and' operation using the symbol '&&', with the 'or' operation using the symbol '||', and with the 'not' operation using the symbol '!'.

Thus a test of suitability for jury service might be: 

((age >= 21)&&(age < 65))

That is: age greater or equal to 21 and less than 65.

4.2 If-else statement

// ex4-2.cpp - if-else statement
#include <iostream>
using namespace std;
int main()
{
    cout << "Enter a number less than 10 : ";
    int value;
    cin >> value;
    if (value < 10) 
        cout << "Thank you" << endl;
    else
        cout << "Can't you follow instructions?" 
             << endl;
    return(0);
}
ex4-2
Enter a number less than 10 : 5
Thank you
ex4-2
Enter a number less than 10 : 12
Can't you follow instructions?

Example ex4-2 demonstrates a variant of the 'if' statement which has a single test condition and two statements.  It has the syntax: 

if (condition) statement1;
  else statement2;

With this form, the condition is evaluated and tested for truth.  If the test condition is true, then statement1 is executed.  If the test condition is false, then statement2 is executed.  In the example, if 'value' contains a number less than 10 then the program prints 'Thank you', otherwise it prints 'Can't you follow instructions'.  You can see that it must always be the case that one of the two statements is performed. 

4.3 Compound if-else statement

// ex4-3.cpp - compound if-else statement
#include <iostream>
using namespace std;
int main()
{
    cout << "Enter a number less than 10 : ";
    int value;
    cin >> value;
    if (value < 10) {
        cout << value << " is less than 10" << endl;
        cout << "Thank you" << endl;
    }
    else {
        cout << value << " is not less than 10" << endl;
        cout << "Can't you follow instructions?"<< endl;
    }
    return(0);
}
ex4-3
Enter a number less than 10 : 5
5 is less than 10
Thank you
ex4-3
Enter a number less than 10 : 12
12 is not less than 10
Can't you follow instructions?

Example 4-3 shows that whole blocks of instructions can be controlled by an 'if' statement, not just a single statement.  In general wherever we can have a single statement, C++ allows us to put a block of statements enclosed in '{' .. '}'.  Thus the syntax of the if statement operating on a block would become: 

if (condition) { 
   statement1   statement2;
   etc
}

and the syntax of the if-else statement operating on two blocks would become: 

if (condition) { 
   statement1   statement2   etc 
}
else { 
   statement3   statement4   etc 
}

In the example, two instructions are executed if 'value' contains a number less than 10, and a different couple of instructions are executed if this is not the case.  Just a reminder here: the layout of the source code with spaces and tabs does not affect its meaning in any way.

4.4 Chained if-else statement

// ex4-4.cpp - chained if-else
#include <iostream>
using namespace std;
int main()
{
    cout << "Enter your age in years : ";
    int age;
    cin >> age;
    if (age < 13)
        cout << "You are a child." << endl;
    else if (age < 20)
        cout << "You are a teenager." << endl;
    else if (age < 65)
        cout << "You are an adult." << endl;
    else
        cout << "You are a senior citizen." << endl;
    return(0);
}
ex4-4
Enter your age in years : 15
You are a teenager.
ex4-4
Enter your age in years : 70
You are a senior citizen.

Example ex4-4 demonstrates how 'if-else' statements can be used to choose between more than two alternatives.  In this example, the program needs to choose between four responses, depending on the numerical value of 'age'.  You should be able to follow the logic here as a 'chain' of tests: if the age is less than 13 then the person is a child, otherwise if the age of the person is less than 20 then the person is a teenager, otherwise if their age is less than 65 they are an adult.  If none of these tests are true, then the person is a senior citizen.  Clearly you have to think carefully about the order in which you do these tests.  What is wrong with this variant:? 

if (age < 20)
    cout << "You are a teenager." << endl;
else if (age < 13)
    cout << "You are a child." << endl;

or this:? 

if (age < 13)
    cout << "You are a child." << endl;
if (age < 20)
    cout << "You are a teenager." << endl;

4.5 Exercises

a. Write a program (inorder.cpp) which reads two numbers and reports whether the two numbers are in ascending size, as in: 
inorder
Enter two numbers  70 30
These numbers are not in ascending order.
inorder
Enter two numbers  20 40
These numbers are in ascending order.
b. Write a program (small.cpp) which reads three numbers and reports the smallest, as in: 
small
Enter three numbers : 17 12 15
The smallest was: 12

© 1999 Mark Huckvale University College London