3.x — Chapter 3 summary and quiz

📘 Chapter 3⏱ about 2 min read✍️ Beginner friendly
Read one box at a time. No rush.

Quick review#

  • + - * / do math. % gives the remainder.
  • x += 5 is short for x = x + 5. ++x adds one.
  • == != < > <= >= compare and give true or false.
  • = puts a value in. == asks if equal.
  • && = and, || = or, ! = not.
  • Use brackets when the order isn't obvious.
Quick check

Pick an answer. You find out right away if it is right.

Quick check #1

What is 17 % 5?

Quick check #2

Which checks if x is between 1 and 10 (inclusive)?

Quick check #3

After int a { 5 }; int b { a++ }; what are a and b?

Quiz time
Question #1

What are the values of these?

  1. 17 % 5
  2. 20 % 4
  3. 3 + 4 * 2
  4. (3 + 4) * 2
  1. 2
  2. 0
  3. 11
  4. 14
Question #2

What does this print?

C++
int x { 5 };x += 3;x *= 2;--x;std::cout << x;

15. 5 + 3 = 8, then 8 × 2 = 16, then 16 − 1 = 15.

Question #3

Write a bool expression that is true when n is between 1 and 100 (including both), or when n is exactly -1.

C++
bool valid { (n >= 1 && n <= 100) || n == -1 };
Question #4

Write a program that asks for a whole number and prints true if it is odd and false if it is even.

C++▶ Run
#include <iostream> int main(){    std::cout << "Enter a number: ";    int n {};    std::cin >> n;    std::cout << std::boolalpha << (n % 2 != 0) << '\n';    return 0;}

We use != 0 instead of == 1 because a negative odd number gives a remainder of -1.

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.