📖 Scenario: You are helping a small shop keep track of the number of items in stock. You will use assignment operators to update the stock count as items are added or sold.
🎯 Goal: Build a Go program that uses assignment operators to update the stock count of a product step-by-step.
📋 What You'll Learn
Create a variable to hold the initial stock count
Create a variable to hold the number of items added or sold
Use assignment operators to update the stock count
Print the final stock count
💡 Why This Matters
🌍 Real World
Shops and businesses often need to update stock counts quickly as items are added or sold.
💼 Career
Understanding assignment operators is essential for writing clear and efficient code in many programming jobs.
Progress0 / 4 steps
1
Create the initial stock count
Create an int variable called stock and set it to 50.
Go
Hint
Use := to declare and assign the variable stock.
2
Create a variable for items added
Create an int variable called added and set it to 20.
Go
Hint
Declare added using := and assign 20.
3
Update stock using assignment operators
Use the += assignment operator to add added to stock.
Go
Hint
Use stock += added to increase stock by added.
4
Print the final stock count
Use fmt.Println to print the value of stock.
Go
Hint
Use fmt.Println(stock) to show the updated stock count.
Practice
(1/5)
1. What does the += operator do in Go?
easy
A. Subtracts the right value from the left variable and stores the result
B. Assigns the right value to the left variable without any operation
C. Adds the right value to the left variable and stores the result in the left variable
D. Multiplies the left variable by the right value and stores the result
Solution
Step 1: Understand the operator meaning
The += operator adds the right-hand side value to the left-hand side variable.
Step 2: Understand the assignment effect
It then stores the sum back into the left-hand side variable, updating its value.
Final Answer:
Adds and assigns the sum to the left variable -> Option C
Quick Check:
+= means add and assign [OK]
Hint: Think: add right value to left and save [OK]
Common Mistakes:
Confusing += with simple assignment =
Thinking it subtracts instead of adds
Mixing it with multiplication or division
2. Which of the following is the correct syntax to multiply variable x by 5 and store the result back in x?