Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Look at the statement int score = 10;. It creates a variable named score and gives it the current value 10. The name is how later code refers to the value. When you write score, C++ looks up the variable named score and uses the value stored there.
int score = 10;
score;In int score = 10;, which part is the variable name, and which value does score currently refer to?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The statement has three important parts. int is the type word, score is the variable name, and 10 is the initial value. The declaration creates score so C++ knows that this variable exists. The initialization gives that newly created variable its first value. One statement performs both actions.
int score = 10;After int score = 10;, score already exists and its current value is 10. The statement score = 15; is a reassignment: it changes the current value of the existing variable. After these two statements run in order, score refers to 15. The variable does not provide access to both 10 and 15, because 10 is no longer its current value.
int score = 10;
score = 15;Replace the second line with the correct reassignment, without declaring score again.
int score = 10;
int score = 15;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Every later use must match the declared name exactly. score and Score are different names because C++ is case-sensitive. A name cannot contain spaces, begin with a digit, or be a reserved language word. If you change the spelling or case when referring to score, C++ does not treat it as the same variable, so the code is rejected when that name has not been declared.
int score = 10;
score = 15;The declaration fixes the identity of the variable, while reassignment changes only its current value. In this example, the identity remains score throughout, and its current value changes from 10 to 15. Keeping those two ideas separate prevents you from mistaking a new declaration for a change to an existing variable.