Essay: Relational Operators & If–Else
In programming, decision-making is essential. A program often needs to choose one path over another— for example, to approve a login, assign a grade, or show a warning. These choices are driven by relational operators working together with the if–else statement.
What are Relational Operators?
Relational operators compare two values and yield a Boolean result: true or false. They do not change the values; they simply test the relationship between them. The most common ones are:
- == (equal to) — checks whether two values are the same.
- != (not equal to) — checks whether two values differ.
- > (greater than) — true when the left value is larger than the right.
- < (less than) — true when the left value is smaller than the right.
- >= (greater than or equal to) — true when the left is larger than or equal to the right.
- <= (less than or equal to) — true when the left is smaller than or equal to the right.
Because a comparison returns only true or false, these operators fit naturally inside conditions.
How If–Else Uses Them
The if
statement evaluates a condition. If it is true, the if
block runs;
otherwise, the else
block runs. Here is a classic example that checks eligibility based on age:
int age = 16; if (age >= 18) { printf("You can vote"); } else { printf("You are too young to vote"); }
In this program, the relational operator >=
compares age
with 18
.
If the result is true, the first message is printed; otherwise, the second.
Why They Matter
Together, relational operators and if–else enable programs to react to data: validate inputs, branch logic, and implement algorithms such as searching and sorting. Without them, code would execute in a straight line with no ability to choose different actions based on conditions.
Scroll down to the interactive section to try the operators live with your own values for a and b.
1) Enter values
Tip: You can use arrow keys while focused in a number field to change values. The comparisons update instantly.
2) What are relational operators?
They compare two values and return a Boolean — either true or false. We’ll evaluate all six common operators below.
- == equal to
- != not equal to
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
3) Live evaluation
4) if–else in action
Under the hood, each condition evaluates to true/false. The if block runs when it’s true; otherwise the else block runs.
5) Assignments (with answers)
Practice these and expand them as mini-programs. Click to show/hide the answers.
-
Compare two numbers: Read
a
andb
. Print which one is larger or if they are equal.Answer
if (a > b) { printf("a is greater"); } else if (a < b) { printf("b is greater"); } else { printf("a and b are equal"); }
-
Equality check: Print
"Equal"
ifa == b
otherwise"Not equal"
.Answer
if (a == b) printf("Equal"); else printf("Not equal");
-
Number in range: Print
"In Range"
ifa
is between 10 and 20 (inclusive).Answer
if (a >= 10 && a <= 20) printf("In Range"); else printf("Out of Range");
-
Absolute difference: Print the absolute difference between
a
andb
using only relational operators and subtraction.Answer
int diff; if (a >= b) diff = a - b; else diff = b - a; printf("%d", diff);
-
Min and Max: Set
max
to the larger ofa
,b
; setmin
to the smaller.Answer
int max, min; if (a >= b) { max = a; min = b; } else { max = b; min = a; }
-
Sign comparison: Print
"Same sign"
ifa
andb
are both non-negative or both negative; else"Different"
.Answer
if ((a >= 0 && b >= 0) || (a < 0 && b < 0)) printf("Same sign"); else printf("Different");
-
Close enough (tolerance): For floating values, print
"Close"
if|a - b| <= 0.001
.Answer
double diff = (a >= b) ? (a - b) : (b - a); if (diff <= 0.001) printf("Close"); else printf("Far");
-
Odd/even relation: If both numbers are even or both odd, print
"Same parity"
; otherwise"Different parity"
.Answer
int aEven = (a % 2 == 0); int bEven = (b % 2 == 0); if ((aEven && bEven) || (!aEven && !bEven)) printf("Same parity"); else printf("Different parity");
-
Grade boundary: Given a mark
a
, print"Pass"
ifa >= 33
else"Fail"
.Answer
if (a >= 33) printf("Pass"); else printf("Fail");
-
Between two numbers: Given
x
, check if it lies betweena
andb
regardless of which is larger.Answer
int low, high; if (a <= b) { low = a; high = b; } else { low = b; high = a; } if (x >= low && x <= high) printf("Yes"); else printf("No");
6) Contact & Progress
If you have questions or want to share your solutions, reach out directly:
Assignments Checklist
6) Track your progress
Select the assignments you’ve completed. Your progress is saved in your browser.
Completed 0/10
0 Comments