⚙️ C++

Arithmetic operations — C++

```html

What is this and why?

Imagine you're playing a computer game. You have an inventory: a sword, a health potion, gold coins. In programming, it's the same — we need somewhere to store the data that the program works with. Variables are like little boxes or containers where you can put a number, a word, or something else. Each box has a name (for example, health_count or hero_name), and you can only put a certain type of thing in it: numbers in one, text in another, yes/no in a third.

Data types are rules that say: "You can only put whole numbers in this box," "And only words in this one." Why is this necessary? So the computer doesn't get confused: if you said the box contains a number, then you can add, subtract, and multiply numbers. But if it's text, you can do other operations with it: concatenate words, search for letters. Without data types, there would be chaos — like if a sword suddenly turned into a potion in a game, and coins started talking.

This is important for a student because any program involves working with data. Want to make a calculator? You need numbers. Want a game with dialogues? You need strings (text). Want to check if a player passed a level? You need boolean values (true/false). Variables and types are the foundation upon which everything else is built.

Syntax and code examples

In C++, you first need to declare a variable — tell the computer: "Listen, I want a box with this name, and it will contain this type of data." This is done like this: first write the data type, then the variable name, then (if necessary) an equals sign and a value.

#include <iostream>  // to be able to output text to the screen
using namespace std;  // so we don't have to write std:: before every word

int main() {
    // Example 1: integer (int)
    int age = 14;  // create a variable "age" of type int (integer) and put 14 in it
    cout << "My age: " << age << endl;  // output to the screen

    // Example 2: floating-point number (double) — for fractions
    double height = 1.65;  // height in meters, can store 1.65
    cout << "My height: " << height << " m" << endl;

    // Example 3: text (string)
    string name = "Alice";  // word or phrase in quotes
    cout << "My name is " << name << endl;

    // Example 4: boolean type (bool) — only true or false
    bool lovesProgramming = true;  // true = 1, false = 0
    cout << "Loves programming? " << lovesProgramming << endl;  // will output 1 (because true)

    // Example 5: character (char) — a single letter or digit in single quotes
    char firstLetter = 'A';
    cout << "First letter of name: " << firstLetter << endl;

    return 0;  // program finished successfully
}

Note: in C++, each expression ends with a semicolon ;. If you forget it, the program won't compile (the computer won't understand where the command ends).

Another example — how variables can be changed:

int score = 0;        // initially 0
score = 10;           // now 10
score = score + 5;    // now 15 (take the old value, add 5)
cout << score << endl;  // will output 15

You cannot change the data type: if you declared int, you cannot put text into it later. The computer will complain.

Comparison with Python

In Python, everything is simpler: you don't need to specify the data type. Python figures out what you mean on its own. But this simplicity comes at the cost of speed and strictness — in C++, you know exactly how much memory each variable occupies, and the program runs faster.

See how the same examples look in Python:

# Python: no need to write int, string, bool — the type is determined automatically
age = 14              # Python understood that this is a number
height = 1.65         # and this is a floating-point number
name = "Alice"        # this is a string
lovesProgramming = True  # boolean type
firstLetter = 'A'     # character (in Python, this is also a string, but with one character)

print("My age:", age)
print("My height:", height, "m")
print("My name is", name)
print("Loves programming?", lovesProgramming)
print("First letter of name:", firstLetter)

Notice the difference? In C++, we write int age = 14;, but in Python, simply age = 14. Python doesn't require type declaration — it looks at the value and decides for itself. But if you try to write int age = "Alice"; in C++, the compiler will give an error. In Python, you can write age = "Alice", and it won't object — but later, if you try to add age to a number, the program will crash at runtime.

Another difference: in C++, you need to include libraries (for example, #include <iostream> for screen output), while in Python, many things are already built-in. For a student, Python is easier to start with, but C++ provides a deeper understanding of how the computer works.

Typical beginner mistakes

  • Forgetting the semicolon. This is the most common mistake. The compiler will say: "Error: expected a semicolon." Just put ; at the end of each command.
  • Using an undeclared variable. If you write cout << x; and the variable x was not declared, the compiler won't understand what it is. Always declare first: int x = 5;
  • Mixing up quotes. For strings (multiple characters), you need double quotes: "Hello". For a single character, use single quotes: 'A'. If you mix them up, you'll get an error.
  • Assigning a value of the wrong type. For example, int a = 3.14; — the compiler may warn that the fractional part will be lost (it will be 3).
```