Relational and logical operators are used to make decisions in programs. They help the computer compare values, check conditions, and control the flow of the program. These operators are widely used in if statements, loops, and conditional expressions.

Relational Operators

Relational operators compare two values and return a Boolean result (true or false). They are essential for decision-making in programs.

Common Relational Operators

OperatorMeaningExample
<Less thana < b
>Greater thana > b
<=Less than or equal toa <= b
>=Greater than or equal toa >= b
==Equal toa == b
!=Not equal toa != b

Using < and >

  • < checks if the left value is smaller than the right.

  • > checks if the left value is greater than the right.

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << (a < b) << endl; // true (1)
cout << (a > b) << endl; // false (0)
return 0;
}

Explanation:

  • (a < b) is true because 10 < 20.

  • (a > b) is false because 10 is not greater than 20.

Using <= and >=

  • <= checks if the left value is smaller or equal to the right.

  • >= checks if the left value is greater or equal to the right.

 
#include <iostream>
using namespace std;
int main() {
int a = 15, b = 15;
cout << (a <= b) << endl; // true (1)
cout << (a >= b) << endl; // true (1)
return 0;
}

Explanation:

  • Both expressions are true because 15 is equal to 15.

Using == and !=

  • == checks if two values are equal.

  • != checks if two values are not equal.

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << (a == b) << endl; // false (0)
cout << (a != b) << endl; // true (1)
return 0;
}

Explanation:

  • (a == b) is false because 10 is not equal to 20.

  • (a != b) is true because 10 is not equal to 20.

Examples of Relational Operators in Decision Making

 
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
else {
cout << "You are not eligible to vote." << endl;
}
return 0;
}
  • Here, the relational operator >= is used to check the eligibility condition.

  • Output depends on the input given by the user.

Logical Operators

Logical operators are used to combine multiple conditions. They are very important in decision-making and loops.

Common Logical Operators

OperatorNameDescriptionExample
&&ANDTrue if both conditions are true(a > 0 && b > 0)
` `OR
!NOTReverses the truth value!(a > b)

Using AND (&&)

  • && returns true only if both conditions are true.

  • Syntax:

 
condition1 && condition2

Example:

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
if (a > 5 && b > 15) {
cout << "Both conditions are true." << endl;
}
return 0;
}

Output:

 
Both conditions are true.
  • Here, both a > 5 and b > 15 are true, so the AND operator returns true.

Using OR (||)

  • || returns true if at least one condition is true.

  • Syntax:

 
condition1 || condition2

Example:

 
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
if (a > 15 || b > 0) {
cout << "At least one condition is true." << endl;
}
return 0;
}

Output:

 
At least one condition is true.
  • Here, a > 15 is false but b > 0 is true, so the OR operator returns true.

Using NOT (!)

  • ! reverses the truth value.

  • Syntax:

 
!condition

Example:

 
#include <iostream>
using namespace std;
int main() {
bool isRaining = true;
if (!isRaining) {
cout << "You can go outside." << endl;
}
else {
cout << "Better to stay inside." << endl;
}
return 0;
}

Output:

 
Better to stay inside.
  • !isRaining becomes false, so the else part executes.

Combining Logical and Relational Operators

  • You can combine relational and logical operators for complex conditions.

 
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 50 && marks <= 100) {
cout << "You passed the exam." << endl;
}
else {
cout << "You failed the exam." << endl;
}
return 0;
}
  • marks >= 50 && marks <= 100 checks if marks are within the passing range.

Examples of Logical Operator Precedence

  • Precedence order in C++:

  1. ! (NOT)

  2. && (AND)

  3. || (OR)

 
#include <iostream>
using namespace std;
int main() {
bool a = true, b = false, c = true;
if (a || b && c) {
// AND evaluated first
cout << "Condition is true." << endl;
}
return 0;
}

Explanation:

  • b && c is evaluated first → false && true = false

  • Then a || false = true → output is true