Loading...

Operators and Expressions in C++

Imagine you’re playing a video game.
Your character collects coins, loses health, gains power, and fights enemies. Behind the scenes, the game constantly calculates things like:

  • health = health – damage
  • score = score + coins
  • levelUp = (score > 2000)
  • isAlive = (health > 0 && lives > 0)

Games run because the computer uses operators and expressions to make decisions and update values.

Today, you’ll learn the same tools games, apps, and robots use to think, compare, and react.

By the end of this lesson, you will build a Mini Game Status Calculator that updates a player's score, health, and level status using operators.

Mini Game Status Calculator

You are designing a small game system.
Your program should:

  1. Start with a score of 50
  2. Add 30 points collected by the player
  3. Reduce health by 10
  4. Check if the player has leveled up (score >= 100)
  5. Check if the player is alive (health > 0)

Your output should look like:

text
7 lines
|
36/ 500 tokens
1
2
3
4
5
6
7
==============================
   GAME STATUS
   Score: 80
   Health: 90
   Level Up: true
   Player Alive: true
==============================

This small project teaches how operators create logic inside real applications.

The Concept: Operators in C++

Operators are symbols that tell the computer what action to perform.

1. Arithmetic Operators

Used for calculations:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % remainder

Example:

cpp
1 lines
|
5/ 500 tokens
1
int score = 50 + 30;
Code Tools

2. Relational Operators

Used to compare values:

  • == equal
  • != not equal
  • > greater
  • < less
  • >= greater or equal
  • <= less or equal

Example:

cpp
1 lines
|
7/ 500 tokens
1
bool isAdult = (age >= 18);
Code Tools

3. Logical Operators

Used for decision-making:

  • && AND
  • || OR
  • ! NOT

Example:

cpp
1 lines
|
13/ 500 tokens
1
bool canEnter = (age >= 18 && hasTicket == true);
Code Tools

4. Increment and Decrement

These update values faster:

  • ++ increases by 1
  • -- decreases by 1

Example:

cpp
2 lines
|
21/ 500 tokens
1
2
score++;   // same as score = score + 1
health--;  // same as health = health - 1
Code Tools

5. Assignment Operators

Used to store and update values:

  • = sets a value
  • += adds and assigns
  • -= subtracts and assigns

Examples:

cpp
2 lines
|
19/ 500 tokens
1
2
score += 10;   // score = score + 10
health -= 5;   // health = health - 5
Code Tools

6. Operator Precedence

Just like math (BODMAS), C++ follows an order:

Multiplication and division happen before addition and subtraction.

Example:

cpp
1 lines
|
12/ 500 tokens
1
int result = 5 + 2 * 3; // result is 11, not 21
Code Tools

Because 2 * 3 happens first.

Let’s Build the Game Status Calculator

Here is the complete C++ program:

cpp
23 lines
|
150/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() {
    int score = 50;
    int health = 100;

    score += 30;     // add points
    health -= 10;    // reduce health

    bool levelUp = (score >= 100);
    bool playerAlive = (health > 0);

    cout << "==============================" << endl;
    cout << "   GAME STATUS" << endl;
    cout << "   Score: " << score << endl;
    cout << "   Health: " << health << endl;
    cout << "   Level Up: " << levelUp << endl;
    cout << "   Player Alive: " << playerAlive << endl;
    cout << "==============================" << endl;

    return 0;
}
Code Tools

Run and see it execute instantly.

Try asking the AI:

“Explain how assignment operators like += and -= work in C++ with examples.”

How It Works

  • Arithmetic operators update your game values.
  • Relational operators check conditions like leveling up.
  • Logical operators combine decisions.
  • Increment/decrement quickly adjust small values.
  • Assignment operators simplify updates.
  • Precedence ensures calculations happen in the right order.

This is how real games make choices thousands of times per second.

Learn Together with AI

Try these prompts:

  • “Write a C++ program that checks whether a number is even or odd.”
  • “Show me how to use logical operators with user input in C++.”
  • “Create a C++ example demonstrating operator precedence.”

AI will help you experiment and learn faster.

Practice Challenge

Your turn!

Create a C++ program that:

  • Starts with coins = 100
  • Player collects 45 coins → add it
  • Player spends 30 coins → subtract it
  • Check:
    • hasEnoughCoins = (coins >= 50)
    • isRich = (coins >= 200)
  • Print the following output:
text
6 lines
|
34/ 500 tokens
1
2
3
4
5
6
==============================
   COIN SYSTEM
   Coins: 115
   Has Enough Coins: true
   Is Rich: false
==============================

Use arithmetic operators, relational operators, and assignment operators.

If you’re stuck, ask AI:

“How do I compare two numbers using relational operators in C++?”

Coming Up Next

In the next lesson, you’ll learn if–else statements, how to make programs decide, just like a real game engine, cashier system, or smart device.

Frequently Asked Questions

You’ll learn how C++ performs calculations and comparisons using arithmetic, relational, logical, and assignment operators, plus how expressions evaluate.

No. You can write and run all C++ programs directly using DevsCall’s AI-powered online code runner.

They perform basic math like addition, subtraction, multiplication, division, and finding remainders.

They compare values (like checking if a score is greater than 100) and return true or false. These comparisons are essential for decisions in your program.

Yes. You can ask AI to generate examples, explain errors, or write short programs using different operators to reinforce your learning.

Still have questions?Contact our support team