Condition checker Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

True and false in C

C has no special true/false type in the classic language: it uses numbers. 0 means false and ANY other value means true. Comparisons give 1 or 0, and the logical operators &&, || and ! combine them — also giving 1 or 0. (stdbool.h adds bool, true and false, but they are still 1 and 0.)

int a = 7, b = 2;

a > b              1   (true)
a == b             0   (false)
a != b             1
a > 5 && b > 5     0   both must be true
a > 5 || b > 5     1   one is enough
!(a > b)           0   NOT
Relational, equality and logical operators (a = 7, b = 2)
Operator Meaning Example Result
< less than a < b 0
> greater than a > b 1
<= less than or equal to a <= 7 1
>= greater than or equal to b >= 3 0
== equal to a == 7 1
!= not equal to a != b 1
&& logical AND: both true a > 5 && b > 5 0
|| logical OR: at least one true a > 5 || b > 5 1
! logical NOT: reverses !(a > b) 0

Comparing values

  1. A comparison gives 1 if it is true and 0 if it is false.
  2. == compares; a single = ASSIGNS. if (a = 5) is always true and changes a.
  3. Characters are compared by their codes: 'a' < 'b' is 1, 'A' < 'a' is 1.
  4. Do not compare decimals with ==: 0.1 + 0.2 == 0.3 is 0. Check the difference instead.
  5. Comparisons cannot be chained: 1 < x < 5 means (1 < x) < 5, which is always 1.

Combining conditions

  1. p && q is true only when p AND q are both true.
  2. p || q is true when p OR q (or both) is true.
  3. !p is true when p is false.
  4. For a range write x > 1 && x < 5 — not 1 < x < 5.
  5. && is done before ||. Use parentheses to make your meaning clear.

Short-circuit evaluation

  1. && and || always evaluate the LEFT side first.
  2. If the left side of && is false, the result is 0 and the right side is NOT evaluated.
  3. If the left side of || is true, the result is 1 and the right side is NOT evaluated.
  4. Use it as a guard: b != 0 && a / b > 2 never divides by zero.
  5. Do not put actions you need (like i++) on the right side — they may never run.
Walkthrough

Storing the answers to questions

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int age = 20, marks = 72;
6 char grade = 'B';
7 int adult = age >= 18; 20 >= 18 is true, so adult is 1.
8 int passed = marks >= 40 && marks <= 100; A range check: both comparisons are true, so passed is 1.
9 int top = grade == 'A' || marks > 90; 'B' == 'A' is 0 and 72 > 90 is 0, so top is 0.
10 int fail = !passed; ! reverses: passed is 1, so fail is 0.
11 int safe = marks != 0 && 100 / marks > 1; The guard comes first. 100 / 72 is 1 (integer division), and 1 > 1 is false: safe is 0.
12 printf("%d %d %d %d %d\n", adult, passed, top, fail, safe); Prints 1 1 0 0 0
13 return 0;
14 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

= instead of ==

✗ Wrong
if (marks = 100)
    printf("Full marks!");
✓ Right
if (marks == 100)
    printf("Full marks!");

marks = 100 stores 100 in marks; its value (100) is true, so the message ALWAYS prints. gcc -Wall warns: "suggest parentheses around assignment used as truth value".

A chained comparison

✗ Wrong
if (1 < x < 5)
✓ Right
if (x > 1 && x < 5)

(1 < x) gives 0 or 1, and both are < 5 — so the condition is always true.

& instead of &&

✗ Wrong
if (a > 0 & b > 0)
✓ Right
if (a > 0 && b > 0)

& is the bitwise AND (next module). It works here by luck but does not short-circuit, and with other values it gives wrong answers: 1 & 2 is 0.

Comparing decimals with ==

✗ Wrong
double total = 0.1 + 0.2;
if (total == 0.3)   // false
✓ Right
double total = 0.1 + 0.2;
if (fabs(total - 0.3) < 1e-9)

0.1 + 0.2 is stored as 0.30000000000000004. Compare the difference with a small tolerance (fabs is in math.h).

Forgetting the parentheses with !

✗ Wrong
if (!age >= 18)
✓ Right
if (!(age >= 18))   // or: age < 18

! binds tighter than >=, so !age >= 18 means (!age) >= 18 — that is 0 or 1 compared with 18, always false.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

What is the value of 5 > 3 ?

Question 2

int a = 4; What is a == 4 && a != 4 ?

Question 3

Which condition is true when x is between 10 and 20 (inclusive)?

Question 4

In b != 0 && a / b > 1 with b = 0, what happens?

Question 5

What is !0 ?

Assessment

Relational & Logical Operators quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.