Selection Statements (if/else)

Control Structures - C++ has three types of control structures

  1. sequence structures - built into C++; unless directed otherwise, computer executes each and every C++ statement one after another, top to bottom (this is all we've done so far)
  2. selection structures - used to chose among alternative courses of action
  3. iteration structures - allow a set of instructions to be repeated until a certain condition is reached; a condition may be open-ended as in a "while" and "do/while" loops or it may be predefined as in the "for" loop (we will cover this later)

Selection Structures

//Program to determine age status
#include <iostream.h>
int main()
{
    int age;

    //Prompt user for age
    cout << endl << "Enter age in years: ";
    cin >> age;

    if (age > 40)
        cout << endl << "A " << age << "year old person is old";
    else
        cout << endl << "A " << age << "year old person is young";

    return 0;
}

Lab 4 Core 1 & Core 2 background & warmup (show debugger also)
show if( i > 10); does nothing

Logical Operators
Nested ifs are useful for double or overlapping conditions
e.g.
if (age >= 0)
    if (age > 40)
        cout << endl << "A " << age << "year old is old";
    else
        cout << endl << "A " << age << "year old is young";

Another way to do this is to use logical operators:
!      NOT (changes true to false, false to true)
&& AND (both conditions must be true)
||      OR (at least one of the condtions must be true)

e.g.
if (age >= 0) && (age > 40)
    cout << endl << "A " << age << " year old person is old";
else
    if (age >= 0) && (age <= 40)
        cout << endl << "A " << age << " year old person is young";

Lab 4 Core 3 background & warmup

Boolean expressions - Operators needed for Lab 4 and their precedence (highest to lowest)
( )
! (NOT)
*, /, %
+, -
<, <=, >, >= (relational operators)
==, != (equality operators)
&& (AND)
||(OR)
= (assignment)

Complete ACTIVITY program using the following specification:
Write a program that displays a suggested activity based on the outdoor temperature (in fahrenheit) and whether or not it is raining/snowing. Use the following table for suggested activities. Show both with nested loops and with logical operators.

Temperature

Raining/Snowing

Activity

>=80

no

beach volleyball

>=80

yes

movie

<80 and >=32

no

running

<80 and >=32

yes

racquetball

<32

no

ice fishing

<32

yes

skiing

"switch" structure ("switch" or "case" statement) - is a multiple selection structure (selects among many different actions); it can only be used for evaluation a constant integral condition (any combination of character constants (single character in quotes) and integer constants (integer value) that evaluates to a constant integer value); the result of the switch statement's expression is compared with each of the case labels; if the comparison with label1 is true, the statement following this case is executed; if the comparison is false, the next case is evaluated and so on; if no match occurs, the default statement (optional) is executed; it is recommended that you end each case structure with a break statement which ensures that only one statement sequence is executed (otherwise "fall-through" occurs with a match and the remaining statements after the match are executed

// Sample program using switch statement.
#include <iostream.h>
int main ()
{
    char letterGrade;

    cout << endl << "Enter a letter grade: ";
    cin >> letterGrade;

    // Execute the selected test.
    switch ( letterGrade )
    {
        case 'A' : cout << "1st case" << endl; break;
        case 'B' : cout << "2nd case" << endl; break;
        case 'C' : cout << "3rd case" << endl; break;
        default : cout << "Invalid input" << endl;
    }

    return 0;
}

Lab 4 Core 4 background & warmup, discuss pseudo-code for application exercise

Copyright Matthew Bauer, Computer Science, Illinois Institute of Technology, Fall 2000