Conditional Expressions (If/Else) and Relational Operators

See pages 115-122 in A Computer Science Tapestry for more coverage of this information and pages 122-126 for additional information. When reading these pages you should substitute character arrays for string statements and ignore the #include "CPstring.h" statement.

Using the expressions that we have incorporated so far, we have not allowed the computer to make any "decisions." That is, when a program is run the computer must execute every line of code that is written. We can allow the computer to adjust to user input by utilizing conditional expressions, so called because their execution is conditional upon certain values. The first conditional expression that we will use is the if/else statement. The basics of this statement are similar to the if-then-else statements of QuickBasic and Visual Basic.

The basic form of the if/else statement is as follows:

if(test expression)
{
    statement list 1
}
else
{
    statement list 2
}

The test expression is usually an equality or inequality that evaluates to either true or false. If it is true the first statement list is executed. If it is false then the second statement list is executed. The else portion of the if/else statement is actually optional. It is valid to have just an if statement. For an explanation of the valid operators in the test expression see page 119 (they are ==, >, <, !=, >= and <=). The most confusing of these is the equals operator (==). This is different than the equals that we use when assigning values to variables (=). This is to differentiate their functions. The single equals (=) assigns values to variables whereas the double equals (==) tests for equality. Here is an example:

int main(void)
{
    int guess;
    cout<<"Enter a guess: ";
    cin>>guess;
    if(guess==4)
    {
        cout<<"Correct!"<<endl;
    }
    else
    {
        cout<<"Incorrect!"<<endl;
    }
    return 0;
}

What does this program do? Try using different expressions in place of guess==4. For example try guess<4, or guess>=4. Now what does the program do?

One particularly useful place for if/else statements is in conjunction with selections from menus. With just if/else statements as outlined above, you could only select from a menu with two choices. This can be extended through multiple if/else if/else statements. The format for these statements is:

if(test expression 1)
{
    statement list 1
}
else if(test expression 2)
{
    statement list 2
}
else
{
    statement list 3
}

This can be used to select from menu choices as in the following example:

int main(void)
{
    int choice;
    cout<<"1. First choice"<<endl;
    cout<<"2. Second choice"<<endl;
    cout<<"3. Third choice"<<endl;
    cout<<"Pick an option from 1 to 3: ";
    cin>>choice;
    if(choice==1)
    {
        cout<<"You picked option 1"<<endl;
    }
    else if(choice==2)
    {
        cout<<"You picked option 2"<<endl;
    }
    else if(choice==3)
    {
        cout<<"You picked option 3"<<endl;
    }
    else
    {
        cout<<"Woops, you should have picked a number from 1 to 3";
    }
    return 0;
}

There is a lot more to learn about if/else statements and the expressions that can be evaluated in them, but for now let's experiment with these forms. Notice the way that the if statments are indented.  This makes it easy to read.  Let's construct a program that allows you to enter a name, and then makes one of four statements using that person's name. The basic structure of this program looks as follows:

void printchoice(char name[20], int choice)
{
    fill in this function
}
int main(void)
{
    char name[20];
    int choice;
    cout<<"Enter your name: ";
    cin>>name;
    cout<<"Which statement would you like "<<name<<"?"<<endl;
    cout<<"1. Tell me happy birthday"<<endl;
    cout<<"2. Tell me I'm the smartest person"<<endl;
    cout<<"3. Tell me I got an A in this course"<<endl;
    cout<<"4. Tell me I'm modest"<<endl;
    cout<<"Make a choice: ";
    cin>>choice;
    printchoice(name, choice);
    return 0;
}

A sample run of this program looks like this:

Enter your name: Eric
Which statement would you like Eric?
1.  Tell me happy birthday
2.  Tell me I'm the smartest person
3.  Tell me I got an A in this course
4.  Tell me I'm modest
Make a choice: 3
Eric got an A in the C++ course

What does this printchoice function need to look like? Make sure that it responds properly to choices other than one to four.

Back to Home Page House3.wmf (25540 bytes)