MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 8 of 11

Block 1 · Unit 2Data Types, Operators and Expressions9 min · Section 8 of 11

2.8Relational and Logical Operators

Covers IGNOU MCS-201 §2.14, 2.15

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

OperatorMeaningExampleValue when a = 5, b = 8
==Equal toa == b0
!=Not equal toa != b1
<Less thana < b1
<=Less than or equal toa <= b1
>Greater thana > b0
>=Greater than or equal toa >= b0

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

OperatorMeaningKind
&&Logical AND: true if both operands are trueBinary
||Logical OR: true if at least one operand is trueBinary
!Logical NOT: reverses true and falseUnary

The logical operators treat 0 as false and any non-zero value as true, and always produce 0 or 1.

xyx && yx || y!x
00001
0non-zero011
non-zero0010
non-zeronon-zero110

Examples: (marks >= 65) && (marks <= 75) tests a range; (pcm >= 85) || (aggregate >= 90) tests either condition.

Short-circuit evaluation

&& 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 1 · multiple choice

What is the value of 7 != 7?

Show answer

Answer: B. 0

7 is equal to 7, so “not equal” is false, which C represents as 0.

Question 2 · multiple choice

What is the value of 5 && 0 || !0?

Show answer

Answer: A. 1

!0 is 1 and && binds tighter than ||: 5 && 0 is 0, then 0 || 1 is 1.

Question 3 · multiple choice

Which condition correctly tests that marks is between 40 and 60 inclusive?

Show answer

Answer: C. marks >= 40 && marks <= 60

Both limits must hold, so && with inclusive comparisons is needed. The chained form compares a 0/1 result with 60 and is always true.

Question 4 · multiple choice

What does this program print?

C · short_circuit.c
#include <stdio.h>

int main(void)
{
    int a = 0, b = 5;
    int r = (a != 0) && (b++ > 2);
    printf("%d %d\n", r, b);
    return 0;
}
Show answer

Answer: D. 0 5

(a != 0) is 0, so && short-circuits and b++ never runs; b stays 5. Expecting 6 forgets short-circuit evaluation.

Question 5 · multiple choice

Which operator has the highest precedence?

Show answer

Answer: C. !

Unary ! binds tighter than any binary operator, which is why !(x == y) needs parentheses.

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.

↑ ↓ move↵ openTab switch type