User Tools

Site Tools


cpp_lesson03

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpp_lesson03 [2019/02/08 23:31]
cpp_lesson03 [2017/11/11 22:47] (current)
Line 1: Line 1:
 +
 +===== Lesson 3: Decisions! Decisions! =====
 +
 +Lesson 3 combines two online classes. These notes supplement the reading.
 +
 +Before you begin, please read:
 +
 +     * [[http://​www.cplusplus.com/​doc/​tutorial/​basic_io/​| Basic Input/​Output]]
 +     * [[http://​www.cplusplus.com/​doc/​tutorial/​control|Control Structures]]
 +
 +==== 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:
 +<​code>​
 +    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;
 +            }
 +        }
 +</​code>​
 +     ​* ​ 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 [[http://​www.cplusplus.com/​doc/​tutorial/​functions/​|Functions I]]
 +
 +==== Reference ====
 +
 +You may be interested in the video tutorial "​Thinking in C". Look for it on [[http://​www.mindviewinc.com/​Index.php|mind view in C]]