Programs make decisions: is the balance enough, is the mark in range, is the user logged in? Relational operators compare values, and logical operators combine those comparisons into complex conditions. In C, every condition is just an integer: 0 means false and any other value means true.
Relational operators
Operator
Meaning
Example
Value when a = 5, b = 8
==
Equal to
a == b
0
!=
Not equal to
a != b
1
<
Less than
a < b
1
<=
Less than or equal to
a <= b
1
>
Greater than
a > b
0
>=
Greater than or equal to
a >= b
0
A relational operator produces 1 if the comparison is true and 0 if it is false. The equality operator is == (two signs); a single = is assignment.
Logical operators
Operator
Meaning
Kind
&&
Logical AND: true if both operands are true
Binary
||
Logical OR: true if at least one operand is true
Binary
!
Logical NOT: reverses true and false
Unary
The logical operators treat 0 as false and any non-zero value as true, and always produce 0 or 1.
&& and || evaluate their left operand first and stop as soon as the answer is known. This is called short-circuit evaluation:
In x && y, if x is false, y is not evaluated (the result must be 0).
In x || y, if x is true, y is not evaluated (the result must be 1).
This matters when the right operand has a side effect or would be unsafe on its own, for example (count != 0) && (total / count > 50) never divides by zero.
Precedence of ! and parentheses
! has higher precedence than the relational and equality operators, so !grade == 'A' means (!grade) == 'A'. Write !(grade == 'A') to negate the comparison. The order is: !, then arithmetic, then < <= > >=, then == !=, then &&, then ||.
Key takeaways
Relational operators compare values and give 1 or 0; equality is ==.
&& needs both conditions true, || needs at least one, ! reverses a condition.
In C, 0 is false and any non-zero value is true.
&& and || short-circuit: the right operand may not be evaluated.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a C condition that is true when a person is eligible to vote (age at least 18) and is either a citizen (citizen == 1) or has special permission (permit == 1). Then evaluate it for age = 20, citizen = 0, permit = 1.
Show worked answer
age >= 18 && (citizen == 1 || permit == 1). The parentheses are needed because && has higher precedence than ||. For age = 20, citizen = 0, permit = 1: age >= 18 is 1; citizen == 1 is 0 and permit == 1 is 1, so the bracket is 0 || 1 = 1; then 1 && 1 = 1. The person is eligible.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-types-operators-and-expressions/relational-and-logical-operators/ to read this page.