Bird
Raised Fist0
C++programming~10 mins

Relational operators in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Relational operators
Start
↓
Evaluate Left Operand
↓
Evaluate Right Operand
↓
Compare using relational operator
↓
Result is true or false
↓
Use result in program
↓
End
Relational operators compare two values and return true or false based on the comparison.
Execution Sample
C++
#include <iostream>

int a = 5;
int b = 3;
bool result = (a > b);
std::cout << result;
This code compares if a is greater than b and prints 1 (true).
Execution Table
StepExpressionLeft OperandRight OperandOperatorComparison ResultOutput
1a > b53>true1
2End-----
💡 Comparison done, result is true because 5 is greater than 3.
Variable Tracker
VariableStartAfter ComparisonFinal
a555
b333
resultundefinedtruetrue
Key Moments - 3 Insights
Why does the comparison 'a > b' return true when a is 5 and b is 3?
Because 5 is greater than 3, the relational operator '>' returns true as shown in execution_table step 1.
What type of value does a relational operator return?
Relational operators return a boolean value: true or false, as seen in the 'Comparison Result' column in the execution_table.
Can relational operators be used with variables of different types?
Yes, but both operands should be comparable types; otherwise, the compiler may give an error or unexpected behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 1?
A5
Bfalse
Ctrue
D3
💡 Hint
Check the 'Comparison Result' and 'Output' columns in execution_table row 1.
At which step does the program finish the comparison?
AStep 1
BStep 2
CStep 3
DStep 0
💡 Hint
Look at the 'Step' column and the 'exit_note' in execution_table.
If 'a' was 2 and 'b' was 3, what would be the comparison result for 'a > b'?
Afalse
B2
Ctrue
D3
💡 Hint
Relational operator '>' returns true only if left operand is greater than right operand.
Concept Snapshot
Relational operators compare two values and return true or false.
Common operators: >, <, >=, <=, ==, !=
Used in conditions to control program flow.
Operands must be comparable types.
Result is a boolean value.
Example: (a > b) returns true if a is greater than b.
Full Transcript
Relational operators in C++ compare two values and return a boolean result: true or false. The common operators include greater than (>), less than (<), greater or equal (>=), less or equal (<=), equal (==), and not equal (!=). In the example, variable 'a' is 5 and 'b' is 3. The expression 'a > b' compares if 5 is greater than 3, which is true. This result is stored in 'result' and printed. Variables 'a' and 'b' remain unchanged. Relational operators are essential for making decisions in programs by checking conditions. They must be used with compatible types to avoid errors.

Practice

(1/5)
1.

Which of the following relational operators checks if two values are not equal in C++?

easy
A. >=
B. !=
C. <=
D. ==

Solution

  1. Step 1: Understand the meaning of each operator

    The operator == checks equality, <= and >= check less or equal and greater or equal respectively.
  2. Step 2: Identify the operator for 'not equal'

    The operator != means 'not equal' in C++.
  3. Final Answer:

    != -> Option B
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Remember: != means not equal in C++ [OK]
Common Mistakes:
  • Confusing == with !=
  • Thinking <= means not equal
  • Mixing up >=
  • Using = instead of ==
2.

Which of the following is the correct syntax to compare if a is greater than b in C++?

int a = 5, b = 3;
if (___) {
    // code
}
easy
A. a > b
B. a => b
C. a >< b
D. a >= b

Solution

  1. Step 1: Recall the correct operator for 'greater than'

    The operator for 'greater than' is >. The operator >= means 'greater than or equal to'.
  2. Step 2: Check syntax correctness

    a > b is the correct syntax to check if a is strictly greater than b. Other options are invalid syntax.
  3. Final Answer:

    a > b -> Option A
  4. Quick Check:

    Greater than operator = > [OK]
Hint: Use > for greater than, >= for greater or equal [OK]
Common Mistakes:
  • Using => instead of >=
  • Using >< which is invalid
  • Confusing >= with >
  • Missing spaces causing syntax errors
3.

What will be the output of the following C++ code?

int x = 7, y = 10;
if (x <= y) {
    std::cout << "Yes";
} else {
    std::cout << "No";
}
medium
A. No
B. 10
C. 7
D. Yes

Solution

  1. Step 1: Evaluate the relational expression

    Check if x <= y means 7 is less than or equal to 10, which is true.
  2. Step 2: Determine which branch executes

    Since the condition is true, the if block runs and prints "Yes".
  3. Final Answer:

    Yes -> Option D
  4. Quick Check:

    7 <= 10 is true, so output is Yes [OK]
Hint: Check condition truth to decide output [OK]
Common Mistakes:
  • Confusing <= with <
  • Assuming else block runs
  • Printing variable values instead of strings
  • Ignoring the condition result
4.

Find the error in the following C++ code snippet:

int a = 4, b = 9;
if (a = b) {
    std::cout << "Equal";
} else {
    std::cout << "Not Equal";
}
medium
A. Incorrect variable declaration
B. Missing semicolon after if statement
C. Using assignment operator instead of equality operator in if condition
D. Using std::cout incorrectly

Solution

  1. Step 1: Identify the operator in the if condition

    The condition uses a = b, which assigns value of b to a instead of comparing.
  2. Step 2: Correct the operator for comparison

    To compare, the operator should be ==, so the condition should be a == b.
  3. Final Answer:

    Using assignment operator instead of equality operator in if condition -> Option C
  4. Quick Check:

    Use == for comparison, not = [OK]
Hint: Use == to compare, = to assign [OK]
Common Mistakes:
  • Using = instead of ==
  • Forgetting to compare variables
  • Assuming assignment returns boolean false
  • Ignoring compiler warnings
5.

Given the code below, what will be the output?

int a = 5, b = 5, c = 10;
if (a == b && c > b) {
    std::cout << "Match";
} else if (a != b || c < b) {
    std::cout << "Mismatch";
} else {
    std::cout << "Unknown";
}
hard
A. Match
B. Unknown
C. Mismatch
D. Compilation error

Solution

  1. Step 1: Evaluate the first if condition

    Check a == b (5 == 5) is true, and c > b (10 > 5) is true, so the whole condition is true.
  2. Step 2: Determine which block executes

    Since the first condition is true, "Match" is printed and the rest is skipped.
  3. Final Answer:

    Match -> Option A
  4. Quick Check:

    Both conditions true, so output is Match [OK]
Hint: Check && needs both true, || needs one true [OK]
Common Mistakes:
  • Confusing && and || operators
  • Ignoring short-circuit evaluation
  • Assuming else if runs after true if
  • Misreading relational comparisons
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard