0
0
C++programming~15 mins

Increment and decrement operators in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Increment and Decrement Operators
📖 Scenario: You are helping a cashier count the number of items in a shopping cart. Sometimes items are added, and sometimes items are removed. You will use increment and decrement operators to keep track of the count.
🎯 Goal: Build a simple C++ program that uses increment (++) and decrement (--) operators to update the count of items in a cart.
📋 What You'll Learn
Create an integer variable itemCount with an initial value.
Create an integer variable itemsAdded to represent how many items are added.
Use the increment operator ++ to increase itemCount by itemsAdded times.
Use the decrement operator -- to decrease itemCount by 1 for each item removed.
Print the final value of itemCount.
💡 Why This Matters
🌍 Real World
Increment and decrement operators are used in many programs to count things, like items in a cart, steps in a process, or scores in a game.
💼 Career
Understanding these operators is essential for writing efficient loops and managing counters in software development jobs.
Progress0 / 4 steps
1
Create the initial item count
Create an integer variable called itemCount and set it to 5 to represent the starting number of items in the cart.
C++
Need a hint?

Use int itemCount = 5; to create the variable.

2
Set the number of items added
Create an integer variable called itemsAdded and set it to 3 to represent how many items are added to the cart.
C++
Need a hint?

Use int itemsAdded = 3; to create the variable.

3
Increase itemCount using increment operator
Use a for loop with variable i from 0 to less than itemsAdded. Inside the loop, use the increment operator ++ on itemCount to increase it by 1 each time.
C++
Need a hint?

Use for (int i = 0; i < itemsAdded; i++) { itemCount++; }

4
Decrease itemCount using decrement operator and print result
Use the decrement operator -- once on itemCount to represent removing one item. Then, print the final value of itemCount using std::cout.
C++
Need a hint?

Use itemCount--; to decrease by one, then std::cout << itemCount << std::endl; to print.