C++ Lecture 3: The ‘Secret Symbols’ that Drive Code Execution

In the previous lecture, we learned how to create variables—these “little boxes” that can store numbers, text, and even boolean values (true/false). However, these “boxes” that can only store data are not very useful; the true magic of programming reveals itself when these “boxes” start to interact, accumulate, compare, and even make judgments. The core of achieving all this lies in operators. They are tiny symbols like “+, -, ==, &&” that enable the code to perform calculations, conditional judgments, and logical control. Without operators, any program is merely a list of stored values. This lecture will guide you through how operators make variables “move”: from simple mathematical calculations to logical judgments, and why understanding operators is crucial for C++ (and all programming languages). Please stick with the learning until the end—this chapter is the bridge connecting “static data” with “real problem-solving”.

C++ Lecture 3: The 'Secret Symbols' that Drive Code Execution

1. Arithmetic Operators (Arithmetic Operators) Arithmetic operators are the basic mathematical tools in programming. Similar to using “+, -” in paper calculations, in C++, we perform calculations on variables using arithmetic operators. Common arithmetic operators include: + : Addition – : Subtraction * : Multiplication / : Division % : Modulus (the remainder after division) C++ example code

#include <iostream>
using namespace std;
int main() {
    // Perform arithmetic operations on integer variables
    int a = 10;
    int b = 3;
    cout << "a + b = " << (a + b) << "\n"; // 10 + 3 = 13
    cout << "a - b = " << (a - b) << "\n"; // 10 - 3 = 7
    cout << "a * b = " << (a * b) << "\n"; // 10 * 3 = 30
    cout << "a / b = " << (a / b) << "\n"; // 10 / 3 = 3 (integer division: no decimal part)
    cout << "a % b = " << (a % b) << "\n"; // Remainder of 10 divided by 3 = 1
    // Perform arithmetic operations on floating-point variables
    float x = 10.0;
    float y = 3.0;
    cout << "x / y = " << (x / y) << "\n"; // 10.0 / 3.0 = 3.3333 (floating-point division: retains decimals)
    return 0;
}

Outputa + b = 13a – b = 7a * b = 30a / b = 3a % b = 1x / y = 3.33333 Key Points 1) When performing division on integer variables (int), the decimal part is discarded (e.g., 10 / 3 = 3). 2) When performing division on floating-point variables (float), the decimal part is retained (e.g., 10.0 / 3.0 = 3.3333). Comparison with Python The usage of arithmetic operators in Python is almost identical, but the syntax is more concise:

a = 10
b = 3
print("a + b =", a + b) # 13
print("a - b =", a - b) # 7
print("a * b =", a * b) # 30
print("a / b =", a / b) # 3.333... (Python division retains decimals by default)
print("a % b =", a % b) # 1

Key Differences 1) In Python, the result of division (/) always retains decimals, regardless of whether the operands are integers. 2) In C++, division retains decimals only when at least one operand is a floating-point type; if both are integers, it performs “integer division” (discarding decimals). 2. Assignment Operators (Assignment Operators) Assignment operators are used to assign values to variables and can sometimes also update the value of the variable. You can think of it as a shortcut for “storing the result of a calculation back into the variable ‘box'”. Common assignment operators include: = : Simple assignment (stores value in variable) += : Add and assign (adds first, then stores, i.e., a += b is equivalent to a = a + b) -= : Subtract and assign (subtracts first, then stores, i.e., a -= b is equivalent to a = a – b) *= : Multiply and assign (multiplies first, then stores, i.e., a *= b is equivalent to a = a * b) /= : Divide and assign (divides first, then stores, i.e., a /= b is equivalent to a = a / b) %= : Modulus and assign (only applicable to integer variables) C++ example code

#include <iostream>
using namespace std;
int main() {
    int a = 10; // "=" assigns 10 to variable a
    cout << "Initial a = " << a << "\n";
    a += 5; // Equivalent to: a = a + 5
    cout << "After executing a += 5, a = " << a << "\n"; // 15
    a -= 3; // Equivalent to: a = a - 3
    cout << "After executing a -= 3, a = " << a << "\n"; // 12
    a *= 2; // Equivalent to: a = a * 2
    cout << "After executing a *= 2, a = " << a << "\n"; // 24
    a /= 4; // Equivalent to: a = a / 4
    cout << "After executing a /= 4, a = " << a << "\n"; // 6
    a %= 5; // Equivalent to: a = a % 5 (modulus operation)
    cout << "After executing a %= 5, a = " << a << "\n"; // 1
    return 0;
}

OutputInitial a = 10After executing a += 5, a = 15After executing a -= 3, a = 12After executing a *= 2, a = 24After executing a /= 4, a = 6After executing a %= 5, a = 1 Comparison with Python The usage of assignment operators in Python is identical (the %= operation on integers is also the same):

a = 10
print("Initial a =", a)
a += 5 # Equivalent to a = a + 5
print("After executing a += 5, a =", a) # 15
a -= 3
print("After executing a -= 3, a =", a) # 12
a *= 2
print("After executing a *= 2, a =", a) # 24
a /= 4
print("After executing a /= 4, a =", a) # 6.0 (Note: Python converts the result to float)
a %= 5
print("After executing a %= 5, a =", a) # 1.0

Key Differences 1) In C++, the result of division on integer variables remains an integer (unless using float/double types). 2) In Python, the result of division (/) is always a float, even if both operands are integers. 3. Comparison Operators (Comparison Operators) Comparison operators are used to compare two values. Unlike arithmetic operators, their return result is not a number, but a boolean value: 1) If the comparison result is “true”, it returns true (represented as 1 in C++). 2) If the comparison result is “false”, it returns false (represented as 0 in C++). Comparison operators are the foundation of “logical judgment” in programming (which will be frequently used when learning if statements later). Common comparison operators include: == : Equal (note: distinguish from the assignment operator “=”, the former is “comparison”, the latter is “assignment”) != : Not equal > : Greater than < : Less than >= : Greater than or equal to <= : Less than or equal to C++ example code

#include <iostream>
using namespace std;
int main() {
    int x = 10;
    int y = 20;
    cout << "x == y: " << (x == y) << "\n"; // False → Output 0
    cout << "x != y: " << (x != y) << "\n"; // True → Output 1
    cout << "x > y : " << (x > y) << "\n"; // False → Output 0
    cout << "x < y : " << (x < y) << "\n"; // True → Output 1
    cout << "x >= 10: " << (x >= 10) << "\n"; // True → Output 1
    cout << "y <= 15: " << (y <= 15) << "\n"; // False → Output 0
    return 0;
}

Outputx == y: 0x != y: 1x > y : 0x < y : 1x >= 10: 1y <= 15: 0 Comparison with Python The comparison operators in Python are identical, but the return results are displayed directly as True or False (instead of 0 or 1):

x = 10
y = 20
print("x == y:", x == y) # False
print("x != y:", x != y) # True
print("x > y:", x > y) # False
print("x < y:", x < y) # True
print("x >= 10:", x >= 10) # True
print("y <= 15:", y <= 15) # False

Key Differences 1) In C++, comparison results are represented by 0 (false) or 1 (true). 2) In Python, comparison results are represented by False (false) or True (true). 4. Logical Operators (Logical Operators) So far, we have only been able to compare “single conditions” (e.g., x > y). However, judgments in real scenarios are often more complex—for example, “Is the user’s age greater than 18 and does he hold a ticket?” This type of “simultaneous judgment of multiple conditions” requires logical operators to implement. Common logical operators include: && : Logical AND (only true if all conditions are true) || : Logical OR (true if at least one condition is true) ! : Logical NOT (turns “true” into “false” and “false” into “true”) Comparison of Symbols in C++ and Python 1) In C++, logical operators are represented by &&, ||, !. 2) In Python, logical operators are represented by and, or, not. Note Since we have not yet learned about “conditional statements” (such as if statements, loops, etc.), we will not expand on the practical application examples of logical operators in this lecture; related content will be supplemented in later chapters. At this stage, just remember: 1) Arithmetic operators: responsible for “mathematical calculations”. 2) Comparison operators: responsible for “posing judgment questions”. 3) Logical operators: responsible for “combining multiple judgments to achieve complex decisions”. Summary In this lecture, we learned about the core symbols that give variables “functionality”: arithmetic operators for mathematical calculations, assignment operators for updating variable values, and comparison operators for conditional judgments, along with a brief introduction to logical operators for combining multiple conditions (their practical application will be explained in the “conditional statements” chapter). With a grasp of operators, the code will no longer be limited to “storing data”—it can perform calculations, comparisons, and prepare for subsequent “logical decisions”.

Leave a Comment