1.x — Chapter 1 summary and quiz
Quick review#
- A statement is one step. Most end with
;. - Every program starts at
main. std::cout <<prints.std::cin >>reads.//starts a comment. The computer ignores it.- A variable is a named box that holds a value of one type.
- Initialize your variables, preferably with
{ }. - Using a variable with no value is undefined behavior.
int / intthrows away the decimal part.
Pick an answer. You find out right away if it is right.
Which line correctly prints Hi and ends the line?
std::cout prints, and << points toward the screen. >> is used with std::cin for reading.
What is the value of x after int x {};?
Empty braces give a sensible starting value. For numbers, that's 0.
Which of these is not a valid variable name?
Names can't start with a number.
What is wrong with this program?
#include <iostream> int main(){ std::cout << "Hello\n" return 0;}The first std::cout line is missing a semicolon ; at the end.
What does this print?
int x { 10 };x = x + 5;x = x * 2;std::cout << x;30. First x is 10. Then 10 + 5 = 15. Then 15 × 2 = 30.
What does std::cout << 9 / 2; print, and why?
It prints 4. Both numbers are int, so the decimal part (.5) is thrown away. Use 9.0 / 2 to get 4.5.
Write a program that asks the user for two whole numbers, then prints their sum and their product.
#include <iostream> int main(){ std::cout << "Enter a number: "; int a {}; std::cin >> a; std::cout << "Enter another number: "; int b {}; std::cin >> b; std::cout << a << " + " << b << " = " << a + b << '\n'; std::cout << a << " * " << b << " = " << a * b << '\n'; return 0;}Tip: press ← / → to move between lessons, / to search.