Skip to main content

Relational and logical operators

Short answer

Relational operators (<, <=, >, >=, ==, !=) compare values and return 1 (true) or 0 (false). Logical operators && (AND) true if both true, || (OR) true if at least one true, ! (NOT) inverts. Example: (a<b)&&(b<c) returns 1 if a<b<c.

Explain relational and logical operators.

Relational operators

They compare two values and give 1 (true) or 0 (false).

OperatorMeaningExample (a=10, b=20)Result
<less thana < b1
<=less than or equala <= b1
>greater thana > b0
>=greater than or equala >= b0
==equal toa == b0
!=not equal toa != b1

Logical operators

They combine conditions and give 1 or 0.

OperatorMeaningRule
&&ANDtrue only if both are true
||ORtrue if at least one is true
!NOTreverses the truth value

Example program:

#include <stdio.h>
int main(void)
{
    int a = 10, b = 20, c = 30;
    printf("a<b: %d, a==b: %d, a!=b: %d\n", a < b, a == b, a != b);
    printf("(a<b)&&(b<c): %d\n", (a < b) && (b < c));
    printf("(a>b)||(b<c): %d\n", (a > b) || (b < c));
    printf("!(a<b): %d\n", !(a < b));
    return 0;
}

Output:

a<b: 1, a==b: 0, a!=b: 1
(a<b)&&(b<c): 1
(a>b)||(b<c): 1
!(a<b): 0

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 3 questions →