Navigation

Shopping cart

There are no products in your shopping cart.

0 Items $0.00

Selection Statements

A selection statement means to chose an action based on a condition. An everyday example is the traffic light. If the light is red, I stop my car, otherwise if the light is green, I go.

In C++, selection statemens work similarly. If some value in memory meets some condition, perform this section of code, otherwise perform the other section of code. There are 2 C++ selection statement available:

  1. If statements
  2. Switch statements

Refer to your textbooks and lecture notes for the details of these. This guide will cover questions like, "Where do I put if statements," and "How do I use and if statement?"

When to use if statements

There's three basic times to use if statements:

  1. Making sure some input value is valid
  2. Chosing which calculation to perform
  3. Deciding which values to output

Testing for valid input

When is the best time to check the data input by a user? Immediately after the program has attempted to input the value. Consider the following algorithm:

  • Input data
    • Input the positive square value
      • Make sure the value is positive

Translating this into C++ looks like this (assuming a variable called "value" has been declared)

cout << "Enter a positive square value: ";

cin >> value;

if(value < 0)

{

cout << "Sorry, the value must be positive." << endl;

return 0;

}

One observation I see often is students attempting to make sure the value is valid before it has ever been input (that is, the if statement happens before the cin >> value statement.) This generally makes the compiler cry comething about uninitialized variables.

Another note: The textbooks have not yet covered anything that would allow your program to gracefully try again. At this point, it's still okay to make your program quit as soon as it encounters something wrong. Later chapters will cover how to make the program try again.

Selecting a calculation to perform

Lets say the program must perform different calculations based on the value of the input. If the input value is below 100, then calculate the square of the value, otherwise calculate the square root. Here's what the algorithm looks like:

  • Perform calculations
    • If the input value is below 100
      • the output value equals the input value squared
    • Otherwise
      • the output value equals the square root of the input value

Now to translate that into C++

if(value < 100)

{

result = value * value;

}

else

{

result = sqrt(value);

}

You'll hear this many times: There's more than one way to solve it.  For the above C++ code, "No, since single statements follow the "if" and the"else", the curly braces aren't needed." Yes, I could have used the conditional operators ?: to shorten it down to

result = value < 100 ? value * value : sqrt(value);

but, most textbooks don't cover the conditional operator yet, and introducing it now might confuse students more than help them.

Choosing ouput

Sometimes you don't want your program to display certain output. Common examples are displaying the remainder in a division only when the remainder is non-zero. Consider the following problem statement:

Write a C++ program that prompts the user to enter the dividend and the divisor. If the user enters an invalid divisor, the program shall display an error message and terminate. The program shall output the quotient, and output the remainder only if the remainder isn't zero.

Here's the algorithm

  • Allocate storage
    • dividend
    • divisor
    • quotient
    • remainder
  • Input data
    • input the dividend
    • input the divisor
      • Make sure the divisor isn't zero.
  • Perform calculations
    • calculate quotient
      • quotient is dividend divided by divisor
    • calculate remainder
      • remainder is dividend modulus by divisor
  • Output results
    • output quotient
    • if remander isn't zero
      • output remainder

Here's the algorithm translated into C++ statements

// Allocate storage

int dividend, divisor, quotient, remainder;

// Input data

cout << "Enter the dividend: ";

cin >> divident;

cout << "Enter the divisor: ";

cin >> divisor;

if(divisor == 0)

{

cout << "Division by zero is not a valid math operation." << endl;

return 0;

}

// Perform calculations

quotient = dividend / divisor;

remainder = dividend % divisor;

// Output results

cout << "The quotient is " << quotient;

if(remainder != 0)

{

cout << " with a remainder of " << remainder;

}

cout << endl;