C++ Seminar: 1094 Session 2

Handouts: (Pick up handouts from Session 1 if you missed it.)

Your goals for today: Quick review of Days 1 and 2 then work through Days 3 and 4.


Review: Days 1 and 2

We'll briefly step through the C++ features used in Days 1 and 2, prompting for questions.

  1. The std "namespace"
  2. cout and cin
  3. Two types of comments: // and /* */
  4. Functions

Day 3: Working with Variables and Constants

You can find more information about these things in the online version of Day 3.

  1. The basic unit for storing information in the computer is the "byte", which is equal to 8 bits (a bit is a 0 or a 1). Download, compile, and run List0301.cpp to see the size in bytes of each of the variable types (you could get different answers on a supercomputer). What is the difference between a float and a double? For scientific work, an integer is almost always declared as an int (without long or short) and numbers with decimal points are almost always declared as doubles. So don't use float unless you have a really good reason (can you guess why?).



  2. Variables are demonstrated in List0302.cpp. The keyword unsigned means that the integer is only positive or zero. Note that a value is assigned to Width when it is declared (we say it is "initialized") but the value of Length is assigned on a separate line. In general, you should always initialize variables to something (often zero), but you can always re-assign the value. Modify the program to add a variable Depth and to calculate the volume. However, don't delete the lines that define and print Area, but "comment them out" using /* and */.



  3. In the last program, why not use W and L as variable names instead of Width and Length, which take longer to type? Try changing Width to width in the line where it is printed out. Why does this fail?



  4. The use of typedef is demonstrated in List0303.cpp. It is a way to define your own name (i.e., an alias) for a variable type. In this example, USHORT replaces unsigned short int. We'll see more interesting uses for typedef later!



  5. Another variable type is char, which is short for "character". Each of the standard characters, i.e., the letters, numbers, and punctuation symbols, has an integer associated with it. A very common set of integer associations is the ASCII code (pronounced "Ask-ee"). Download, compile, and run List0306.cpp to see how the characters associated with the numbers from 32 to 127. The statement (char) i converts the integer i to the corresponding ASCII character. It is unlikely you will need to explicitly use the ASCII code; this is for cultural enrichment! Try changing 127 to a larger number (but less than 256).



  6. Here are some of the Day 3 Quiz questions to try (answers are in the handout from Session 1):
    1. What is the difference between an integer variable and a floating-point variable?
    2. What makes for a good or bad variable name?
    3. Which of the following variable names are good, which are bad, and which are invalid?

      Age,   !ex,   R79J,   TotalIncome,   __Invalid

Day 4: Creating Expressions and Statements

Your goal is to learn about blocks, expressions, how to "branch" your code with "if" statements, and how to handle the truth. You can find more information about these things in the online version of Day 4.

  1. Take a look at List0403.cpp, which demonstrates "prefix" and "postfix" increment and decrement operators. If I set int x = 3, then x++ or ++x will both result in the value of x changed to 4. It doesn't matter which you use if the statement is on a line by itself. However, if they are part of a more complicated statement (like the cout statements in the example), there is a difference. Can you figure out the rule? The bottom line here is that the behavior can be quite confusing, so the best rule is: ONLY use x++ on a line by itself. Why do you think the language is called "C++"?



  2. Try replacing ++ by -- in the previous example. What is the rule? Another way to add one to a variable is x += 1. This generalizes trivially to adding two: x += 2 or to multiply by two: x *= 2. Test these out.



  3. The example in List0404.cpp demonstrates the use of if statements. The basic form is
     if (expression)
     {
       statement1;
       statement2;
       statement3;
     }
    where expression is something that is either true or false and the statements can be any C++ statements (if there is only one, you can omit the {}'s, but we advise using them in any case). In the example, there are comparisons of scores. It's probably easy to interpret > and <, but if you want to check if two numbers are equal, you use == and NOT = (using = in this case is one of the most common beginner's error in C or C++). Can you trace the logic of the program?



  4. The example in List0405.cpp introduces the else clause (if "expression" is true do this, else do that). Try adding another if-else combination that checks if the numbers are equal. If they are, print "They are equal" and if not, print "They are not equal."



  5. The example in List0406.cpp shows a more complicated example. The interesting new feature here is the % operator. The expression x % y is true if y evenly divides x and false otherwise (so it is the modulo operator). Try it out!



  6. There are C++ operators corresponding to the logical operators AND, OR, and NOT. The symbols are &&, ||, and ! respectively. Here are two possible uses:
     if ( (x == 1) && (y == 2) )
     {
       cout << " x is 1 and y is 2 " << endl;
     }
     
     if ( (x == 1) || (y == 2) )
     {
       cout << "x is 1 OR y is 2 " << endl; 
     }
    Try these out in one of the examples.



  7. Here are some of the Day 4 Quiz questions to try (answers are in the handout from Session 1):
    1. What is the value of 201/4?
    2. What is the value of 8+2*3?
    3. What is the difference between if (x=3) and if (x==3)?




  8. Download Ex0402.cpp but don't compile it yet. First look at the program, imagine entering three numbers, and by following through the "if" statements, write down what output you predict. Then compile and run it, and check your result. If you got it wrong, go back and try again.



  9. Download Ex0404.cpp but don't compile it yet. Look at the program and anticipate the output. Then check.

C++ Seminar: 1094 Session 2. Last modified: 10:48 pm, November 05, 2006.
furnstahl.1@osu.edu