DEV Community

Mehfila A Parkkulthil
Mehfila A Parkkulthil

Posted on

Day 7 : C++ language | Comparison Operators

Comparison operators

Comparison operators are used to compare two values and return a boolean result (true or false).
They are fundamental in programming for making decisions and controlling the flow of execution.


Here are those:

Equal to (==)
Purpose: Checks if the two values are equal.
Syntax: a == b

Example:
int a = 5;
int b = 5;
if (a == b) {
    // This block will execute  code because a is equal to b
}
Enter fullscreen mode Exit fullscreen mode

Not equal to (!=)
Purpose: Checks if the two values are not equal.
Syntax: a != b

Example:
int a = 5;
int b = 3;
if (a != b) {
    // This block will execute code because a is not equal to b
}

Enter fullscreen mode Exit fullscreen mode

Greater than (>)
Purpose: Checks if the value on the left is greater than the value on the right.
Syntax: a > b

Example:
int a = 5;
int b = 3;
if (a > b) {
    // This block will execute because a is greater than b
}

Enter fullscreen mode Exit fullscreen mode

Less than (<)
Purpose: Checks if the value on the left is less than the value on the right.
Syntax: a < b

Example:
int a = 3;
int b = 5;
if (a < b) {
    // This block will execute because a is less than b
}

Enter fullscreen mode Exit fullscreen mode

Greater than or equal to (>=)
Purpose: Checks if the value on the left is greater than or equal to the value on the right.
Syntax: a >= b

Example:
int a = 5;
int b = 5;
if (a >= b) {
    // This block will execute because a is equal to b
}

Enter fullscreen mode Exit fullscreen mode

Less than or equal to (<=)
Purpose: Checks if the value on the left is less than or equal to the value on the right.
Syntax: a <= b


Example:
int a = 3;
int b = 5;
if (a <= b) {
    // This block will execute because a is less than b
}

Enter fullscreen mode Exit fullscreen mode

Practical Applications

Conditional Statements

Conditional statements will be explained in upcoming blog .
Comparison operators are commonly used in conditional statements to control the flow of execution.

Example:

#include <iostream>
using namespace std ;

int age = 18;

if (age >= 18) {
    printf("You are eligible to vote.\n");
} else {
    printf("You are not eligible to vote.\n");
}

Enter fullscreen mode Exit fullscreen mode

In this example:

The condition age >= 18 checks if the value of age is greater than or equal to 18.
The appropriate message is printed based on the result of the comparison.

Loops

Loops will be explained in upcoming blog .
Comparison operators are also used in loops to determine the continuation or termination of the loop.

Example:
for (int i = 0; i < 10; i++) {
    printf("%d ", i);  // Prints numbers from 0 to 9
}

Enter fullscreen mode Exit fullscreen mode

Previous Blogs

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay