3.x — Chapter 3 summary and quiz
Quick review#
+ - * /do math.%gives the remainder.x += 5is short forx = x + 5.++xadds one.== != < > <= >=compare and givetrueorfalse.=puts a value in.==asks if equal.&&= and,||= or,!= not.- Use brackets when the order isn't obvious.
Pick an answer. You find out right away if it is right.
What is 17 % 5?
5 goes into 17 three times (15), with 2 left over. % gives the remainder.
Which checks if x is between 1 and 10 (inclusive)?
C++ can't chain comparisons like math. Use && — both parts must be true. (|| would be true for every number!)
After int a { 5 }; int b { a++ }; what are a and b?
a++ gives the old value (5) to b, then adds one to a.
What are the values of these?
17 % 520 % 43 + 4 * 2(3 + 4) * 2
201114
What does this print?
int x { 5 };x += 3;x *= 2;--x;std::cout << x;15. 5 + 3 = 8, then 8 × 2 = 16, then 16 − 1 = 15.
Write a bool expression that is true when n is between 1 and 100 (including both), or when n is exactly -1.
bool valid { (n >= 1 && n <= 100) || n == -1 };Write a program that asks for a whole number and prints true if it is odd and false if it is even.
#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.
Tip: press ← / → to move between lessons, / to search.