Relational operator in c




Relational Operators in C




Table of Contents



  1. Introduction

  2. List of Relational Operators

  3. Explanation of Relational Operators

  4. Examples and Programs

  5. Conclusion




Introduction


Relational operators in C are used to compare two values and determine the relationship between them. These operators help us check if one value is equal to, not equal to, greater than, less than, greater than or equal to, or less than or equal to another value. The result of a relational operation is either true (1) or false (0).


In C, relational operators are essential for decision-making in conditional statements and loops. They play a crucial role in controlling the flow of a program and making logical decisions based on certain conditions.




List of Relational Operators































Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to



Explanation of Relational Operators


a) == (Equal To Operator)


The == operator checks whether the left-hand side (LHS) is equal to the right-hand side (RHS). If the values on both sides are equal, the result is true (1), and if they are not equal, the result is false (0).



b) != (Not Equal Operator)


The != operator checks whether the LHS is not equal to the RHS. If the values on both sides are not equal, the result is true (1), and if they are equal, the result is false (0).



c) > (Greater Than Operator)


The > operator checks whether the LHS is greater than the RHS. If the value on the left side is greater than the value on the right side, the result is true (1), and if it is not greater, the result is false (0).



d) < (Less Than Operator)


The < operator checks whether the LHS is less than the RHS. If the value on the left side is less than the value on the right side, the result is true (1), and if it is not less, the result is false (0).



e) >= (Greater Than or Equal To Operator)


The >= operator checks whether the LHS is greater than or equal to the RHS. If the value on the left side is greater than or equal to the value on the right side, the result is true (1), and if it is not greater than or equal to, the result is false (0).



f) <= (Less Than or Equal To Operator)


The <= operator checks whether the LHS is less than or equal to the RHS. If the value on the left side is less than or equal to the value on the right side, the result is true (1), and if it is not less than or equal to, the result is false (0).




Examples and Programs



Example 1: Using == (Equality Operator)




#include <stdio.h>

int main() {
int num1 = 5, num2 = 5;

if (num1 == num2) {
printf("num1 is equal to num2\n");
} else {
printf("num1 is not equal to num2\n");
}

return 0;
}


Output:


num1 is equal to num2


Example 2: Using != (Not Equal Operator)




#include <stdio.h>

int main() {
int num1 = 10, num2 = 5;

if (num1 != num2) {
printf("num1 is not equal to num2\n");
} else {
printf("num1 is equal to num2\n");
}

return 0;
}


Output:


num1 is not equal to num2


Example 3: Using > (Greater Than Operator)




#include <stdio.h>

int main() {
int num1 = 10, num2 = 5;

if (num1 > num2) {
printf("num1 is greater than num2\n");
} else {
printf("num1 is not greater than num2\n");
}

return 0;
}


Output:


num1 is greater than num2


Example 4: Using < (Less Than Operator)




#include <stdio.h>

int main() {
int num1 = 5, num2 = 10;

if (num1 < num2) {
printf("num1 is less than num2\n");
} else {
printf("num1 is not less than num2\n");
}

return 0;
}


Output:


num1 is less than num2


Example 5: Using >= (Greater Than or Equal To Operator)




#include <stdio.h>

int main() {
int num1 = 10, num2 = 10;

if (num1 >= num2) {
printf("num1 is greater than or equal to num2\n");
} else {
printf("num1 is not greater than or equal to num2\n");
}

return 0;
}


Output:


num1 is greater than or equal to num2


Example 6: Using <= (Less Than or Equal To Operator)




#include <stdio.h>

int main() {
int num1 = 5, num2 = 10;

if (num1 <= num2) {
printf("num1 is less than or equal to num2\n");
} else {
printf("num1 is not less than or equal to num2\n");
}

return 0;
}


Output:


num1 is less than or equal to num2



Conclusion


Relational operators in C are powerful tools to compare values and make logical decisions based on the result. Understanding how these operators work is crucial for writing effective and efficient C programs. Use them wisely to control the flow of your program and make the most out of conditional statements and loops. Happy coding!





Post a Comment

0 Comments