Welcome to Lesson 8!


Learning Objectives

By the end of today's class, you should know...
  • How do you handle decision points involving exactly two alternatives using an else clause?
  • How do you compare data of the char and string types?
  • How to make decisions when there are multiple alternatives?
  • What is the proper ordering of if statements when order of the statements matter?

Announcements

  • Midterm 1 one week from today
    • Study over your old quizzes, in-class review activities and homework
    • On material through if statements 
    • Midterm review posted as homework tonight (practice quiz)
    • Material from Lesson 2 through if statements (Lesson 8)
  • Quiz 3 after the break
  • Don't forget Lab 4 due Friday!


Review Activity

With a partner, answer the following questions:

1. Write one line of code to declare a String variable (name of your choice) and assign it the value $.

2. Write one line of code to declare a char variable (name of your choice) and assign it the value of $.

3. What does the following output to the console:

string info = "Why hello, I'm Emily!";

cout << info.length() << ": " << info.substr(1,1) << info.substr(11, 1) << info.substr(14,3);

4. Write an if statement to check if the value stored in the variable age is equal to 5. If so, print out the age.



Using if-else Statements

  • Sometimes we want to choose between two actions
  • If a condition is true
    • then do this
  • Otherwise it is false
    • so do something else
  • To make this type of selection we use an if...else statement
  • Syntax:
    if (test) {
       statements1
    } else {
       statements2
    }
    
  • Where:
    • test: the test condition to evaluate
    • statementsX: the statements to execute depending on the test


C++ if...else statement

(from http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm)


  • For example:
    if (7 == guess) {
        cout << "*** Correct! ***\n";
    } else {
        cout << "Sorry, that is not correct.\n";
        cout << "Try again.\n";
    }
    
  • ****Note that there is no test condition for the else clause****
  • The decision on which set of statements to use depends on only one condition
  • Note that you could write an if-else as a pair of complementary if statements instead, like:
    if (7 == guess) {
        cout << "*** Correct! ***\n";
    }
    if (7 != guess) {
        cout << "Sorry, that is not correct.\n";
        cout << "Try again.\n";
    }
    
  • However, it is easier and clearer to write an if-else statement instead
  • For clarity, write the if and else parts on different lines than the other statements
  • Also, indent the other statements
  • You can see an example of an if-else statement in the following example

Example Program With an if-else Statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;


int main() {
    int guess = 0;
    cout << "I'm thinking of a number between"
         << " 1 and 10.\nCan you guess it?\n\n"
         << "Enter your guess: ";
    cin >> guess;

    if (7 == guess) {
        cout << "*** Correct! ***\n";
    } else {
        cout << "Sorry, that is not correct.\n";
        cout << "Try again.\n";
    }
    return 0;
}

Activity 8.1: Is Your Number Even or Odd? (10pts)

  • Open up your program from the prior activity: Is Your Number Even?
  • Now add an else clause to display when the number is odd.
  • When your program is able to display both options (even and odd), submit it to Canvas.
  • Both partners need to submit for full credit.


Comparing Characters and Strings

  • Character data can be evaluated using relational operators as well
  • Comparing characters works because C++ stores characters as numbers using ASCII codes
  • Note that letters nearer to the start of the alphabet have lower numerical values
  • Thus a numerical comparison can decide the alphabetical order of characters

Example Program Comparing Characters

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

int main() {
    cout << boolalpha; // output true or false
    cout << "'A' < 'B': " << ('A' < 'B') << endl;
    cout << "'A' > 'B': " << ('A' > 'B') << endl;
    cout << "'A' <= 'Z': " << ('A' <= 'Z') << endl;
    cout << "'X' >= 'Y': " << ('X' >= 'Y') << endl;
    cout << "'X' == 'X': " << ('X' == 'X') << endl;
    cout << "'X' != 'X': " << ('X' != 'X') << endl;
}

Comparing Strings

  • We can compare strings using relational operators as well
  • C++ compares two strings using lexicographical order (a.k.a. alphabetic order)
  • For example, "car" is less than "cat":
    car
    cat
  • Also, "car" is less than "card"
    car
    card

Example Program Comparing Strings

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;

int main() {
    string s1, s2;
    cout << "Enter two strings: ";
    cin >> s1 >> s2;
    cout << boolalpha; // output true or false
    cout << s1 << " <= " << s2 << " : " << (s1 <= s2) << endl;
    cout << s1 << " > " << s2<< " : " << (s1 > s2) << endl;
}


Activity 8.2: Let's Alphabetize! (10 pts)

  • Open up Eclipse and create a new C++ project Alphabetize with a file called alphabetize.cpp.
  • Our program will take in two string inputs from the user, compare then and the output the two strings in alphabetical order.
  • At the top of your program, declare a string variables called word1.
string word1;
  • Now declare a second string variable called word2.
  • Next, write a cout statement welcoming your user to the program and letting them know that this program will alphabetize two words.
cout << "Welcome! Give me two words and I will return them to you in alphabetical order!\n";
  • Run your program to make sure it is giving you the output you expected.
  • Let's prompt the user for the first word and store the result as word1.
cout << "Please enter the first word: ";
cin >> word1;

  • Do the same for the second word.
  • Now, let's create an if-else statement to determine the ordering of the two words. And, then output the result to our user. The if-else statement will need to use string comparison as discussed above.

  • The code that you have written could be a useful part of a larger program. 
  • Both partners should submit to Canvas when finished.



Wrap Up
  • Find a partner and answer the questions from today's learning objectives

Upcoming Assignments