🚀
ArkyEdu GLOSAR TIC
RO Practice Lab
Algorithms, C++ & Scratch Grades 7–8 RO Term: Atribuire (=) vs. Comparare Egalitate (==) în C++

Assignment (=) vs. Equality Check (==) in C++

💬 Share on WhatsApp
📌 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!

❓ 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)