---
title: "Relational and logical operators"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:relational-logical-operators"
source: https://www.bctengineer.com/q/semester_1/computer_programming/relational-logical-operators
site: BCT Engineering
---

# Relational and logical operators

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2073 Shrawan, 4 marks
- Video: [Relational and Logical Operators | C Programming for Beginners](https://www.youtube.com/watch?v=dgCNzGrlEp4) — JNJ - Coding, Gaming and Bird Watching
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/relational-logical-operators
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

Explain 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.

## Answer

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

| Operator | Meaning | Example (a=10, b=20) | Result |
|---|---|---|---|
| `<` | less than | `a < b` | 1 |
| `<=` | less than or equal | `a <= b` | 1 |
| `>` | greater than | `a > b` | 0 |
| `>=` | greater than or equal | `a >= b` | 0 |
| `==` | equal to | `a == b` | 0 |
| `!=` | not equal to | `a != b` | 1 |

#### Logical operators
They combine conditions and give 1 or 0.

| Operator | Meaning | Rule |
|---|---|---|
| `&&` | AND | true only if both are true |
| `\|\|` | OR | true if at least one is true |
| `!` | NOT | reverses the truth value |

Example program:
```c
#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:
```text
a<b: 1, a==b: 0, a!=b: 1
(a<b)&&(b<c): 1
(a>b)||(b<c): 1
!(a<b): 0
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What value does a relational operator give?
   A. The larger operand
   B. 1 for true or 0 for false
   C. The difference of operands
   D. The text true or false
   Answer: B. Relational operators give 1 if the relation holds, otherwise 0. (Key point: Result of relational)

2. With a=10, b=20, what are a>b and a!=b?
   A. 1 and 1
   B. 0 and 0
   C. 1 and 0
   D. 0 and 1
   Answer: D. 10 > 20 is false (0); 10 != 20 is true (1). (Key point: a<b example)

3. When is a || b true?
   A. Only if both are true
   B. Only if both are false
   C. If at least one is true
   D. If exactly one is true
   Answer: C. || is true if at least one operand is true. (Key point: OR operator)

4. With a=10, b=20, what is !(a<b)?
   A. 1
   B. 0
   C. 10
   D. -1
   Answer: B. a<b is 1, and ! reverses it to 0. (Key point: NOT operator)

## Related in Computer Programming

- [Relational and assignment operators](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Arelational-assignment-operators) — Computer Programming, chapter 3

## Related in other subjects

- [Relational operators comparing lengths](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoverload-relational-length) — Object Oriented Programming, chapter 5
- [Distance class relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adistance-relational-overload) — Object Oriented Programming, chapter 5
- [Complex numbers relational operators](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acomplex-relational-overload) — Object Oriented Programming, chapter 5
