📖 Scenario: You are helping a small shop owner keep track of the number of items in stock. You will use simple assignment and compound assignment to update the stock count as items are added or sold.
🎯 Goal: Build a small C# program that creates a stock count variable, updates it using assignment and compound assignment operators, and then prints the final stock count.
📋 What You'll Learn
Create an integer variable called stockCount with an initial value of 50.
Create an integer variable called newShipment with a value of 20.
Use the assignment operator = to set stockCount to 50.
Use the compound assignment operator += to add newShipment to stockCount.
Use the compound assignment operator -= to subtract 15 from stockCount.
Print the final value of stockCount.
💡 Why This Matters
🌍 Real World
Keeping track of stock counts is important for shops to know how many items they have available.
💼 Career
Understanding assignment and compound assignment is a basic skill for programming jobs that involve data updates and calculations.
Progress0 / 4 steps
1
Create the initial stock count
Create an integer variable called stockCount and set it to 50.
C Sharp (C#)
Hint
Use int to declare a whole number variable and = to assign the value.
2
Add new shipment count
Create an integer variable called newShipment and set it to 20.
C Sharp (C#)
Hint
Declare another integer variable and assign it the value 20.
3
Update stock count with compound assignment
Use the compound assignment operator += to add newShipment to stockCount.
C Sharp (C#)
Hint
Use stockCount += newShipment; to add and assign in one step.
4
Subtract sold items and print final stock
Use the compound assignment operator -= to subtract 15 from stockCount. Then print stockCount using Console.WriteLine.
C Sharp (C#)
Hint
Use stockCount -= 15; to subtract 15. Then print with Console.WriteLine(stockCount);.
Practice
(1/5)
1. What does the compound assignment operator += do in C#?
easy
A. Multiplies the left variable by the right value
B. Subtracts the right value from the left variable
C. Adds the right value to the left variable and assigns the result to the left variable
D. Divides the left variable by the right value
Solution
Step 1: Understand the operator meaning
The += operator adds the right side value to the left side variable.
Step 2: Assign the result back
The result of the addition is stored back into the left variable.
Final Answer:
Adds the right value to the left variable and assigns the result to the left variable -> Option C
Quick Check:
+= means add and assign [OK]
Hint: Remember: += means add then assign in one step [OK]
Common Mistakes:
Confusing += with subtraction or multiplication
Thinking += just adds without assignment
Using += on constants (which is invalid)
2. Which of the following is the correct syntax to multiply a variable x by 5 using a compound assignment in C#?