Basic Operators in C Programming Language
(Cont)
Relational Operators or Comparison Operators
These are used for comparison of the values of
two operands.
For example, checking if one operand is equal to
the other operand or not, an operand is greater
than the other operand or not etc. Some of the
relational operators are (==, >= , <= ).
Example1: == (Equal to)
#include <stdio.h>
int main()
{
int x=5;
int y=3;
if (x==y)
{
printf("True\n" );
}
else
{
printf("False\n" ); // return false because 5 is not equal to 3
}
}
Example1: != (Not equal)
#include <stdio.h>
int main()
{
int x=5;
int y=3;
if (x!=y)
{
printf("True\n" );
}
else
{
printf("False\n" ); // return true because 5 is not equal to 3
}
}
Example1:>(Greater than )
#include <stdio.h>
int main()
{
int x=5;
int y=3;
if (x>y)
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns true because 5 is greater than 3
}
}
Example1:<(Less than )
#include <stdio.h>
int main()
{
int x=5;
int y=3;
if (x<y)
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns false because 5 is not less than 3
}
}
Example1: >=(Greater than or equal to)
#include <stdio.h>
int main()
{
int x=5;
int y=3;
if (x>=y)
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns true because 5 is greater, or equal, to 3
}
}
Example1:<=(Less than or equal to)
#include <stdio.h>
int main()
{
int x=5;
int y=3;
if (x<=y)
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns false because 5 is neither less than or equal to 3
}
}
Logical Operators
Logical operators are used to determine the logic
between variables or values.
Example1: && (Logical and)
#include <stdio.h>
int main()
{
int x=5;
if( x> 3 && x < 10)
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns true because 5 is greater than 3 AND 5 is less than
10
}
Example1: || (Logical or)
#include <stdio.h>
int main()
{
int x=5;
if( x> 3 || x < 4)
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns true because one of the conditions are true (5 is
greater than 3, but 5 is not less than 4)
}
Example1: !(Logical not)
#include <stdio.h>
int main()
{
int x=5;
if(!(x> 3 || x < 10))
{
printf("True\n" );
}
else
{
printf("False\n" ); // returns false because ! (not) is used to reverse the result