In C, relational operators are used to compare two values and determine the relationship between them.
These operators return a Boolean value, which is either true (represented as 1) or false (represented as 0),
indicating whether the specified relationship holds true or not. Relational operators are commonly used in
conditional statements to make decisions based on the result of the comparison.
Here are the relational operators in C:
Equality operator (==):
Syntax: expression1 == expression2
Description: Checks if expression1 is equal to expression2. It returns true if both expressions
have the same value, and false otherwise.
Example:
if (x == y) {
// This block will execute if 'x' is equal to 'y'.
}
Inequality operator (!=):
Syntax: expression1 != expression2
Description: Checks if expression1 is not equal to expression2. It returns true if the expressions
have different values, and false if they are equal.
Example:
if (x != y) {
// This block will execute if 'x' is not equal to 'y'.
}
Greater than operator (>):
Syntax: expression1 > expression2
Description: Checks if expression1 is greater than expression2. It returns true if
expression1 is greater, and false otherwise.
Example:
if (x > y) {
// This block will execute if 'x' is greater than 'y'.
}
Less than operator (<):
Syntax: expression1 < expression2
Description: Checks if expression1 is less than expression2. It returns true if expression1 is smaller,
and false otherwise.
Example:
if (x < y) {
// This block will execute if 'x' is less than 'y'.
}
Greater than or equal to operator (>=):
Syntax: expression1 >= expression2
Description: Checks if expression1 is greater than or equal to expression2. It returns true if
expression1 is greater or equal, and false otherwise.
Example:
if (x >= y) {
// This block will execute if 'x' is greater than or equal to 'y'.
}
Less than or equal to operator (<=):
Syntax: expression1 <= expression2
Description: Checks if expression1 is less than or equal to expression2. It returns true if expression1 is
smaller or equal, and false otherwise.
Example:
if (x <= y) {
// This block will execute if 'x' is less than or equal to 'y'.
}
Relational operators are fundamental for comparing values and making decisions in C programs. They are
often used in conjunction with conditional statements (e.g., if, else if, switch) to control the flow of the program based on specific conditions.