Relational Operators

Relational Operators & if–else — Interactive Blog
Relational Operators × if–else
An interactive blog post — type values for a and b, and see which comparisons are true vs false.

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

==
a == b
!=
a != b
>
a > b
<
a < b
>=
a >= b
<=
a <= b

4) if–else in action

Choose a condition to see which branch runs.

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.

  1. Compare two numbers: Read a and b. 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");
    }
  2. Equality check: Print "Equal" if a == b otherwise "Not equal".

    Answer
    if (a == b) printf("Equal"); else printf("Not equal");
  3. Number in range: Print "In Range" if a is between 10 and 20 (inclusive).

    Answer
    if (a >= 10 && a <= 20) printf("In Range"); else printf("Out of Range");
  4. Absolute difference: Print the absolute difference between a and b using only relational operators and subtraction.

    Answer
    int diff;
    if (a >= b) diff = a - b; else diff = b - a;
    printf("%d", diff);
  5. Min and Max: Set max to the larger of a, b; set min to the smaller.

    Answer
    int max, min;
    if (a >= b) { max = a; min = b; }
    else        { max = b; min = a; }
  6. Sign comparison: Print "Same sign" if a and b 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");
  7. 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");
  8. 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");
  9. Grade boundary: Given a mark a, print "Pass" if a >= 33 else "Fail".

    Answer
    if (a >= 33) printf("Pass"); else printf("Fail");
  10. Between two numbers: Given x, check if it lies between a and b 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:

💬 Contact on WhatsApp

Assignments Checklist

6) Track your progress

Select the assignments you’ve completed. Your progress is saved in your browser.

Completed 0/10

Built for learners: clean, responsive, keyboard-friendly. Drop this file into any site or Blogger post (as an HTML gadget) to teach comparisons interactively.

Post a Comment

0 Comments

Me