Recall & Review
beginner
What is the purpose of the assignment operator (=) in C++?
The assignment operator (=) is used to copy the value from the right side to the variable on the left side.
Click to reveal answer
beginner
Explain the combined assignment operator += with an example.
The operator += adds the right side value to the left side variable and assigns the result back to the left variable.<br>Example:
int x = 5; x += 3; // x becomes 8Click to reveal answer
beginner
List three compound assignment operators in C++ besides +=.
Three compound assignment operators are:<br>- -= (subtract and assign)<br>- *= (multiply and assign)<br>- /= (divide and assign)
Click to reveal answer
intermediate
What happens if you write
a = b = c; in C++?This is called chained assignment. It assigns the value of
c to b, then assigns b's value to a. Both a and b end up with the value of c.Click to reveal answer
intermediate
Why should you be careful when using the assignment operator inside conditions?
Using assignment (=) inside conditions can cause bugs because it assigns a value instead of comparing. For comparison, use ==. For example,
if (x = 5) assigns 5 to x and always evaluates true, which is usually unintended.Click to reveal answer
What does the operator
*= do in C++?✗ Incorrect
The operator *= multiplies the left variable by the right value and stores the result back in the left variable.
What is the result of this code?<br>
int a = 2; a += 3;✗ Incorrect
The operator += adds 3 to a's current value 2, so a becomes 5.
Which operator is used to assign a value to a variable?
✗ Incorrect
The single equals sign (=) is the assignment operator in C++.
What does this code do?<br>
int x = 1, y = 2; x = y = 5;✗ Incorrect
The assignment is chained: y is assigned 5, then x is assigned y's value, so both become 5.
Why is this condition problematic?<br>
if (a = 10)✗ Incorrect
Using = assigns 10 to a, so the condition is always true, which is usually a mistake.
Explain how compound assignment operators work and give two examples.
Think about how they combine an operation and assignment in one step.
You got /2 concepts.
Describe what happens in chained assignments and why it might be useful.
Consider how values flow from right to left.
You got /3 concepts.