CIT020 Index > Lecture Notes - Chapter 2

Chapter 2

Understanding Truth

In Table 2.1, note that you use two equal signs to test if two values are equal. The single equal sign is a command: put a value into a variable, as in:

average = (score1 + score2) / 2.0;

The two equal signs ask a question: “are these two values equal or not?” so you will normally use it in the context of an if statement.

if (nLives == 1)

Warning! If you use a single equal sign in an if expression, the compiler will not complain, but the program will almost certainly not do what you want. Try this program, and you’ll see what we mean.

int number = 0;
if (number = 5)
{
   cout << "If statement says true. Number is " << number << endl;
}

Interpreting a Value as True or False

On page 39, the book gives an example of testing to see if the value of scoreis non-zero:

if (score)
   cout << "Okay, at least you didn't score zero.\n\n";

There are three things to change with this example, at least according to the class programming standards. First, don’t use \n for a newline; use endl instead.

Second, you must always enclose the consequence of the if or an else in braces; that is, you must always make a block. You do this for two reasons: a) you will eventually modify your code and add more statements, and you’d have to add the braces then anyway. b) If you always use braces, you will never have the problem described on pages 42 and 43, because the placement of the braces will make your meaning clear.

Third, and most important, score is a numeric value, so you will should a numeric comparison rather than taking advantage of the “anything non-zero is true” rule. As written, a negative score is non-zero, so it would also pass the test. Use a variable name without a relational operator only when the variable is inherently boolean (yes/no, true/false).

Here is an example of these principles at work:

int score = 0;
bool canJump = false;
 /* code to update score goes here */
if (score > 0) // numeric comparison, use relational operator
{
   cout << "Okay, at least you didn't score zero." << endl
      << endl;
}

if (canJump) // boolean comparison; don't use relational operator
{
   cout << "Press the 'j' key to jump." << endl;
}
else
{
   cout << "Your jumping energy is gone." << endl;
}

Nested if Statements

You can also nest if statements inside an else clause.

int age = 0;
cout << "What is your age? ";
cin >> age;
if (age < 18)
{
   cout << "You cannot vote." << endl;
}
else
{
   cout << "You can vote." << endl;
   if (age < 35)
   {
      cout << "Sorry, you cannot be President yet." << endl;
   }
   else
   {
      cout << "You could be our next President, too!" << endl;
   }
}

The switch Statement

If you’ve ever done river rafting, you know that the trip does not have to begin at the place where the river begins and does not have to end where the river empties out into the ocean. Instead, there are specific places where you can enter the river and places where you exit, depending upon how wild a ride you want.

The switch statement is like a river. The condition in parentheses after the switch keyword tells which entry point to use. The case statements are entry points to the river, and once you enter, you keep going downstream until you hit a break statement, which is your exit point from the river.

You will always want to end each case section with a break. Unlike the book, you will want to put a break at the end of the default: case. It’s not necessary, but it makes your code consistent.

If you ever do want to flow from one case into another, then leave off the break and comment the code so that anyone reading it will be warned. In the following example, we show a case without a break. Notice that you may use char variables as well as int variables for a switch.

char studentType = ' ';
cout << "Enter student type: ";
cin >> studentType;
switch (studentType)
{
   case 'e':
      cout << "Elementary" << endl;
      break;
   case 'j':
      cout << "Junior ";
      // no break -- continues to next case
   case 'h':
      cout << "High School" << endl;
      break;
   case 'c':
      cout << "College" << endl;
      break;
   default:
      cout << "Unknown student type" << endl;
      break;
}

It is also possible to associate multiple cases that enter the “river” at the same place. The following is a good example of an example, not necessarily a good example of how to achieve this programming goal.

int n = 0;
cout << "Enter a number from 1 to 10: ";
cin >> n;
switch ( n )
{
   case 1:
       cout << "The loneliest number." << endl;
       break;
   case 3:
   case 6:
   case 9:
       cout << "That is a multiple of three." << endl;
       break;
   case 5:
   case 10:
       cout << "That is a multiple of five." << endl;
       break;
   case 2:
   case 7:
       cout << "That is a prime number." << endl;
       break;
   case 8:
       cout << "Nothing special about eight." << endl;
       break;
   default:
       cout << "Invalid entry." << endl;
       break;
}