0
0
C++programming~5 mins

Assignment operators in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 8
Click 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++?
ADivides the left variable by the right and assigns the result
BSubtracts the right value from the left variable
CAdds the right value to the left variable
DMultiplies the left variable by the right and assigns the result
What is the result of this code?<br>int a = 2; a += 3;
Aa is 5
Ba is 6
Ca is 3
Da is 2
Which operator is used to assign a value to a variable?
A=
B+=
C==
D!=
What does this code do?<br>int x = 1, y = 2; x = y = 5;
AOnly x becomes 5
Bx and y both become 5
COnly y becomes 5
Dx becomes 2, y becomes 5
Why is this condition problematic?<br>if (a = 10)
AIt compares a to 10
BIt checks if a is not equal to 10
CIt assigns 10 to a and always evaluates true
DIt causes a syntax error
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.