Assignment (=) vs. Equality Check (==) in C++
📌 Quick Definition (Answer-First)
A single equals sign (=) is the assignment operator that stores a value into a variable (x = 5). Double equals (==) is the comparison operator that tests whether two values are equal (x == 5).
How It Works & Why It Matters
Mistaking = for == inside an if condition is the single most common bug in middle-school C++.
Writing if (x = 0) overwrites x with 0 instead of checking whether x equals 0.
💻 Assignment vs. Relational Operators
x = 10; // ASSIGNMENT: stores 10 into x if (x == 10) // EQUALITY: tests if x equals 10 if (x != 10) // NOT EQUAL && (AND), || (OR), ! (NOT)
🏫 Classroom Example
Always write if (x >= 1 && x <= 10) in C++ rather than mathematical chaining 1 <= x <= 10.
⚖️ = (Command) vs. == (Question)
= changes the variable’s stored value; == asks a true/false question without altering the variable.
⚠️ Common Mistake in Exams & Homework
Chaining inequalities like 5 < x < 10 in C++ — (5 < x) evaluates to true (1) or false (0), which is always < 10!
🚀 Practice in ArkyEdu Labs & Related Guides
🎮 Launch Interactive Lab →
Structura Alternativă: Instrucțiunea if / else & Operatori Logici
📖 Full Lesson & Curriculum →
Clasa a VII-a · Decizii if / else în C++
🏫 Grade 7 Hub →
All modules & guides for this grade
🏫 Grade 8 Hub →
All modules & guides for this grade
📚 Top 10 C++ Errors Fixed →
Troubleshoot the 10 most common C++ mistakes in Grades 7–8 (Code::Blocks): = vs ==, stray semicolons after if/while, infinite loops, 5/2 integer division, and n = 0.
❓ Quick Questions & Answers
Why does 1 <= x <= 10 fail in C++ when x = 50?
Because 1 <= 50 evaluates to true (1), and then 1 <= 10 is also true, giving a wrong answer!
Related Glossary Terms
← Back to Full Glossary (A–Z)Conditional Branching (if / else & switch) in C++ →
Conditional branching lets a program choose between different execution paths depending on whether a logical condition is true or false (if / else), or by matching discrete constant values (switch).
Variable & C++ Data Types (int, double, char, bool) →
A variable is a named container in RAM that holds a value which can change while a program runs. In C++, every variable must be declared with a specific data type before use.
Algorithm & Core Algorithm Properties →
An algorithm is a finite, unambiguous, step-by-step set of instructions that transforms input data into the desired output within a finite number of steps.
Modulo (%) & Integer Division (/) in C++ →
When both operands are integers in C++, / computes the integer quotient (discarding decimals), while modulo % returns the remainder. Together with base 10 (% 10 and / 10), they power digit-extraction and divisibility algorithms.