Comparison Operators in Programming
Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:
Operator | Description | Examples |
---|---|---|
== (Equal to) | Checks if the values on both sides are equal. | 5 == 5; (evaluates to true) |
!= (Not equal to) | Checks if the values on both sides are not equal. | 10 != 5; (evaluates to true) |
< (Less than) | Tests if the value on the left is less than the value on the right. | 3 < 7; (evaluates to true) |
> (Greater than) | Tests if the value on the left is greater than the value on the right. | 10 > 8; (evaluates to true) |
<= (Less than or equal to) | Checks if the value on the left is less than or equal to the value on the right. | 5 <= 5; (evaluates to true) |
>= (Greater than or equal to) | Checks if the value on the left is greater than or equal to the value on the right. | 8 >= 8; (evaluates to true) |
These operators are extensively used in conditional statements, loops, and decision-making constructs to control the flow of a program based on the relationship between variables or values. Understanding comparison operators is crucial for creating logical and effective algorithms in programming.
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 5, c = 10;
if (a == b)
cout << a << " is equal to " << b << endl;
if (a != c)
cout << a << " is not equal to " << c << endl;
if (a < c)
cout << a << " is less than " << c << endl;
if (a <= b)
cout << a << " is less than or equal to " << b << endl;
if (c > a)
cout << c << " is greater than " << a << endl;
if (a >= b)
cout << a << " is greater than or equal to " << b << endl;
return 0;
}
/* package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 5, b = 5, c = 10;
if (a == b)
System.out.println(a + " is equal to " + b);
if (a != c)
System.out.println(a + " is not equal to " + c);
if (a < c)
System.out.println(a + " is less than " + c);
if (a <= b)
System.out.println(a + " is less than or equal to " + b);
if (c > a)
System.out.println(c + " is greater than " + a);
if (a >= b)
System.out.println(a + " is greater than or equal to " + b);
}
}
a = 5
b = 5
c = 10
if a == b:
print(f"{a} is equal to {b}")
if a != c:
print(f"{a} is not equal to {c}")
if a < c:
print(f"{a} is less than {c}")
if a <= b:
print(f"{a} is less than or equal to {b}")
if c > a:
print(f"{c} is greater than {a}")
if a >= b:
print(f"{a} is greater than or equal to {b}")
function main() {
let a = 5, b = 5, c = 10;
if (a === b)
console.log(a + " is equal to " + b);
if (a !== c)
console.log(a + " is not equal to " + c);
if (a < c)
console.log(a + " is less than " + c);
if (a <= b)
console.log(a + " is less than or equal to " + b);
if (c > a)
console.log(c + " is greater than " + a);
if (a >= b)
console.log(a + " is greater than or equal to " + b);
}
main();
Output
5 is equal to 5 5 is not equal to 10 5 is less than 10 5 is less than or equal to 5 10 is greater than 5 5 is greater than or equal to 5
Types of Operators in Programming
Types of operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables, constants, or values, and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.
Table of Content
- Types of Operators in Programming
- Arithmetic Operators in Programming
- Comparison Operators in Programming
- Logical Operators in Programming
- Assignment Operators in Programming
- Increment and Decrement Operators in Programming
- Bitwise Operators in Programming