6.x — Chapter 6 summary and quiz

Quick review

Always use parentheses to disambiguate the precedence of operators if there is any question or opportunity for confusion.

The arithmetic operators all work like they do in normal mathematics. The remainder (%) operator returns the remainder from an integer division.

The increment and decrement operators can be used to easily increment or decrement numbers. Avoid the postfix versions of these operators whenever possible.

Beware of side effects, particularly when it comes to the order that function parameters are evaluated. Do not use a variable that has a side effect applied more than once in a given statement.

The comma operator can compress multiple statements into one. Writing the statements separately is usually better.

The conditional operator (?:) (also sometimes called the arithmetic if operator) is a ternary operator (an operator that takes 3 operands). Given a conditional operation of the form c ? x : y, if conditional c evaluates to true then x will be evaluated, otherwise y will be evaluated. The conditional operator typically needs to be parenthesized as follows:

  • Parenthesize the entire conditional operator when used in a compound expression (an expression with other operators).
  • For readability, parenthesize the condition if it contains any operators (other than the function call operator).

Relational operators can be used to compare floating point numbers. Beware using equality and inequality on floating point numbers.

Logical operators allow us to form compound conditional statements.

Quiz time

Complete the following program:

#include <iostream>

// Write the function getQuantityPhrase() here

// Write the function getApplesPluralized() here

int main()
{
    constexpr int maryApples { 3 };
    std::cout << "Mary has " << getQuantityPhrase(maryApples) << ' ' << getApplesPluralized(maryApples) << ".\n";

    std::cout << "How many apples do you have? ";
    int numApples{};
    std::cin >> numApples;

    std::cout << "You have " << getQuantityPhrase(numApples) << ' ' << getApplesPluralized(numApples) << ".\n";
 
    return 0;
}

Sample output:

Mary has a few apples.
How many apples do you have? 1
You have a single apple.

getQuantityPhrase() should take a single int parameter representing the quantity of something and return the following descriptor:

  • < 0 = “negative”
  • 0 = “no”
  • 1 = “a single”
  • 2 = “a couple of”
  • 3 = “a few”
  • > 3 = “many”

getApplesPluralized() should take a single int parameter parameter representing the quantity of apples and return the following:

  • 1 = “apple”
  • otherwise = “apples”

This function should use the conditional operator.

Show Hint

Show Solution

guest
Your email address will not be displayed
Find a mistake? Leave a comment above!
Correction-related comments will be deleted after processing to help reduce clutter. Thanks for helping to make the site better for everyone!
Avatars from https://gravatar.com/ are connected to your provided email address.
Notify me about replies:  
104 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments