Chapter 6 Margin Notes
General Note: In this chapter, we learn how to write programs that can make decisions. This capability is the heart and soul of programming.
Page 210. Let's not get carried away with this notion of structures---it's a highly theoretical Computer Science thing. Sequence Structures are what you've already learned--code that runs from top to bottom. Selection Structures are what you are learning in this chapter---primarily If statements. Iteration Structures are what you'll learn in Chapter 7---loops.
Page 211. Boolean operators---you remember George Boole, don't you?
Page 211, Relational Operators. The author states that "Relational operators allow two quantities to be compared"---I'm not thrilled with that explanation. Relational operators allow two operands to be compared---they're not always quantities. Also, note the author's statement that in general you should only compare data of the same data type--this is extremely important in C++. Note the exception the author gives that says that Integers can be compared to floating point data.
Page 211, Example 6.1 Take a look at this---it might show up in a quiz somewhere.
Page 211, Table 6.1. Don't confuse the 'equal to' operator (==) with the assignment operator (=). Despite this warning, you will--everyone does.
Page 212, Example e. Take a look at this---how can A be 'less than' Z? The answer is that the comparison is done using the underlying numeric values of the character. A comes before Z in the alphabet, and so A has been assigned a lower numeric value than Z. Check it out by executing this code
#include <iostream>
using namespace std;
int main() {
cout << 'A' << endl;
cout << (int)'A' << endl;
cout << 'Z' << endl;
cout << (int)'Z';
}
Page 212. When
Relational operations are combined with arithmetic operators within an
expression, the relational operators are always performed LAST.
Page 213. Check out that debugging tip! That may be true, but this program works...
#include <iostream>
using namespace std;
int main() {
float a = 1.2F;
float b = 1.2F;
cout << (a == b); //a and b are equal
}
Page 214. Notice for the OR Table, that for
the 4 possible combinations, 3 result in true and 1 in false. In fact, the only
way an OR operation can be False is if both operands are False.
Page 215. Notice for the AND Table, that for the 4 possible combinations, 3 result in false and 1 in true. In fact, the only way an AND operation can be True is if both operands are True.
Page 215. Be sure that you use a double || for OR and a double && for AND. The single versions have a special meaning for C++.
Page 216, Problem solving in Action. This program mentions the NAND operator. Don't worry about that for this course. In fact, skip right to page 219.
Page 219, The if Statement. Finally!
Page 220. If you're confused by the format of the If statement, code one up!
Page 220. Notice how there is NO SEMICOLON at the end of the line that begins with if!
Page 220. Although the author notes that a curly brace should be on a separate line, it doesn't have to be. This code
if (a == 13)
{
cout << ("a is 13");
}
can also be written like this (and frequently is...)
if (a == 13) {
cout << ("a is 13");
}
and since there's just one
statement to be executed if the test expression is true, we could also write it
like this
if (a == 13) cout << ("a is 13");
Page 221. Check out the Programming Note, and the two Debugging Tips.
Page 222-233. I reviewed all of these--did you?
Page 224, the if/else statement. Let me simplify this. With the simple If statement, one or more statements are executed if the test expression is True--suppose you want, in addition to doing something if the test expression is true, you also want to do something if the test expression is false? Those are the statements that you include in the 'else' block.
Page 227, Comparing Strings. It probably never occurred to you that comparing Strings is any different than comparing primitive data types---so ignore this section.
Page 228, Nested ifs. If you can, avoid these at all costs!
Page 235. The Selector variable must either be an Integer or a variable that evaluates to either an Integer or a char.
Page 235. This is important---if a given case statement matches, if it does not include a break statement as its final statement, each subsequent case block will be executed as if it were true. Check out this code and tell me what will be displayed in the Console window...
#include <iostream>
using namespace std;
int main() {
int a = 13;
switch (a)
{
case 13:
cout << ("a is 13") << endl;
case 1:
cout << ("a is 1") << endl;
} // end of switch
} // end of main
What about now?
#include <iostream>
using namespace std;
int main() {
int a = 13;
switch (a)
{
case 13:
cout << ("a is 13") << endl;
break;
case 1:
cout << ("a is 1") << endl;
break;
} // end of switch
} // end of main