πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Comparison & Logical Operators

C comparison operators (==, !=, <, >) and logical operators (&&, ||, !).

🎯 Why study this lesson?
To write "if … then …" (conditional branching with if) or "while … keep going" (loops with while / for), the comparison and logical operators in this lesson are essential tools.
You'll lean on them in every lesson from here on out. Without them, a program can't really "decide" or "repeat" anything.
πŸ’­ In plain English
"If the age is 18 or over, allow voting."
"If the password is correct and the account is active, log the user in."
"While there's stock left, keep selling."
βš™οΈ Written in C
if (age >= 18) // comparison
if (pass_ok && active) // logical AND
while (stock > 0) // loop condition
Goal of this lesson: get comfortable with ==, !=, >=, and combining them via &&, ||, and ! so you can write any condition you need.
πŸ“– What to learn on this page
βœ… Must-know essentials
  • ==, !=, <, >, <=, >= compare values
  • && (and), || (or), ! (not)
  • A comparison result is 0 (false) or 1 (true)
⭐ Read if you have time
  • Short-circuit: the right side is skipped when the result is known
  • & (bitwise AND) vs && (logical AND)
  • Avoid == on floats β€” use an epsilon

Comparison Operators — result is true (1) or false (0)

A comparison evaluates to the integer 1 (true) or 0 (false).
OperatorMeaningResult for a=3, b=5
a == bEqual0 (false)
a != bNot equal1 (true)
a > bGreater0 (false)
a >= bGreater or equal0 (false)
a < bLess1 (true)
a <= bLess or equal1 (true)
Try it:
Result
β€”

Logical Operators — Combining Conditions

OperatorMeaningReads asExample
A && BA AND B (both true)ANDage >= 18 && age < 65
A || BA OR B (either true)ORday == 0 || day == 6
!ANOT ANOT!finished

Truth Tables

AND (&&)

ABA && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

OR (||)

ABA || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

== vs = — Critical!

Classic bug: writing if (a = 10) when you meant ==. This assigns 10 to a and then uses 10 as the condition β€” which is always true.
// BUG
if (a = 10) {  // = is assignment! a becomes 10 and condition is always true
  printf("Always runs\n");
}

// correct
if (a == 10) {  // == is comparison. True only when a equals 10
  printf("a is 10\n");
}
Memory hook: == means "are these equal?"; = means "put this into that".

Ternary Operator (? :)

The ternary operator compresses a simple if-else into a single expression.
Syntax: condition ? value_if_true : value_if_false
// using if-else
int max;
if (a > b) {
    max = a;
} else {
    max = b;
}

// one line with ternary
int max = (a > b) ? a : b;
Common patterns:
UseExampleMeaning
Max of two values(a > b) ? a : bpick a if it's larger, otherwise b
Absolute value(x >= 0) ? x : -xx if non-negative, otherwise -x
Even / odd label(n%2==0) ? "even" : "odd"drop straight into a printf
Avoid divide-by-zero(b != 0) ? a/b : 0divide if b is nonzero, otherwise use 0
When to reach for it: great for simple two-way assignments or inline printf arguments. For anything complex, plain if-else is more readable.
Avoid nesting: chains like (a>b) ? ((a>c)?a:c) : ((b>c)?b:c) are a headache to read β€” use if-else instead.

Try It Yourself — Comparison & Logic

cmp.c
Output
Press "Run" to execute...
πŸ’‘ A few things to try

Related Lessons

Conditionals
if Statement
How to write if statements in C, with flowcharts for visual understanding.
Operators
Assignment & Increment
C assignment operators and increment (++) / decrement (--).
Operators
Arithmetic Operators
Visual guide to C arithmetic operators (+, -, *, /, %), including integer division pitfalls.
← Previous lesson
Lesson 10: Assignment & Increment
Next lesson →
Lesson 13: if Statement

Review Quiz

Check your understanding of this lesson!

Q1. Which C operator tests equality?

=
==
===

= is assignment; == is the equality (comparison) operator. === doesn't exist in C β€” that's a JavaScript operator.

Q2. What is !(1 && 0) ?

0 (false)
1 (true)
Error

1 && 0 evaluates to 0 (false), and negating that gives !0 = 1 (true).

Q3. What is 5 > 3 && 2 > 4 ?

1 (true)
0 (false)
Error

5 > 3 is true (1); 2 > 4 is false (0). && is true only if both sides are, so 1 && 0 = 0 (false).

Share this article
Share on X (Twitter) Share on Facebook Share on LINE Hatena Bookmark