User Tools

Site Tools


cpp_lesson03

Lesson 3: Decisions! Decisions!

Lesson 3 combines two online classes. These notes supplement the reading.

Before you begin, please read:

If statement

  • It is more professional to use braces even with just one statement.
  • When you are writing a compound if statements you use boollian logic "and" or "or". You write these as "&&" and "||". For example:
  if ((x >= 5) && (x <= 10))
  if ((x > 10) || (x < 5))
  • Use "else" when the condition described in the "if" statement is not true.
  • As with "if", enclose the statement following "else" in braces.

Assignment for the If Statement

grade point average program

Write a program to receive the student's grade point average. Write a response to that gpa, e.g., 4.0 "You're a genious!".

Switch Statement

  • Switch works with labels and only works with constants. Let's say "x" is an integer from 1 to 10. For example:
    switch (x) {

    case 1: {
        cout << "The value is 1";
        break;
        }
    case 2: {
        cout << "The value is 2";
        break;
        }
    case 3: {
        cout << "The value is 3";
        break;
        }
        // the case statements continue in this same pattern until
        case 10: {
            cout << "The value is 10";
            break;
            }
        }
  • If you put a label without a break the label would be identified but the same actions would be taken for all the labels before a break. In our previous example, if the "break ;" did not occur between labels "1" and "2", both value messages would be printed for "case 1". "case 2:" would be properly identified.
  • When you have exhausted all your "case" statements, you may want to include a "default:" statement. This will cover all the values you did not make a special "case" statement for.
  • Using our previous example you might want to write:
  cout << "The value is greater than 10";
  • There is no "break;" in " default:".

Goto

It's best not to use this statement.

Assignment

-1.Write a program to include the following:

  • Ask the user for the current date. This includes the day, month and 4-digit year.
  • Ask the user for their birth date. This also includes day, month, and 4-digit year.
  • Calculate how old they are based on the information given.
  • Be sure to test to see if the user has already had their birthday.
  • Include a "switch" statement in this assignment.
  • Remember leap years.
  • Perform error checking for month, day and year.
  • If it is the user's birthday, wish them a "happy birthday".
  • You can also make remarks about their age.

-2.Please read Functions I

Reference

You may be interested in the video tutorial "Thinking in C". Look for it on mind view in C

cpp_lesson03.txt · Last modified: 2013/07/03 14:27 (external edit)