cpp Logical and comparison operators

Greeting! Students, Today We will learn C++ Logical Operators and Comparison Operators and will cover all steps.

That help you to understand C++ deeply. then follow the all steps.

C++ Comparison Operators

Comparison Operators are using to compare two or more values.

So, Remember, it will return the value with true (1) or false (0).

In the Following Example, we are using the Greater Than Operator (>) to find out the 10 is greater than 6.

So, lets see the Example:

#include <iostream>
using namespace std;

int main(){
     int a = 10;
     int b = 6;
     cout << (a > b); // Will Return it True (1) value
     return 0;
}

Then, look at Output:

1

So, this program, will return true (1) because 10 is greater than 6.

In the next section, the list of Comparison Operators:

OperatorNameExample
==So, this is using as Equal tox == y
!=Not Equal tox != y
>Greater thanx > y
< Less thanx < y
>=Greater than or equal to x >= y
<= Less than or equal tox <= y
C++ Comparison Operators
#include <iostream>
using namespace std;

int main(){
     int a = 10;
     int b = 6;
     cout << "A and B are Equal: " << (a == b) << endl;
     cout << "A and B are not Equal: " << (a != b) << endl;
     cout << "A and B are Greater Than: " << (a > b) << endl;
     cout << "A and B are Less Than: " << (a < b) << endl;
     cout << "A and B are greater than or Equal: " << (a >= b) << endl;
     cout << "A and B are greater than or Equal: " << (a <= b) << endl;
	 return 0;
}

So, see the Output:

A and B are Equal: 0
A and B are not Equal: 1
A and B are Greater Than: 1
A and B are Less Than: 0
A and B are greater than or Equal: 1
A and B are greater than or Equal: 0

So, you will learn more about comparison operators, in the next chapters of Loops. Because the comparison operator has a big part in C++.

Today Our topic is Two C++ Logical Operators and Comparison Operators.

C++ Logical Operators are using to define the logic between variables and values. So, there are three types of Operators. In the below

1. Logical AND (&&)

Operator number One is double and signs like this &&. its called logical and. So This operator returns the value true if both values or conditions are true. Example below:

#include <iostream>
using namespace std;

int main(){
  int x = 6;
  cout << (x > 2 && x < 10); 
  return 0;
}
Output:
1

So, This example returns true (1) because 6 is greater than 2, AND 6 is less than 10.

2. Logical OR (||)

Operator number Two is double or signs like this || . its called logical OR. So This operator returns the value true if one of both values or conditions is true. Example below:

#include <iostream>
using namespace std;

int main(){
  int x = 6;
  cout << (x > 7 || x < 10); 
  return 0;
}
Output:
1

So, This example returns true (1) because 6 is less than 10 and one condition is true.

2. Logical NOT (!)

Operator number Three is a single Exclamation sign (!) . its called logical NOT. So This operator returns the reverse value. if both are true. it will return false. Example below:

#include <iostream>
using namespace std;

int main(){
  int x = 6;
  cout << (!(x > 2 && x < 10)); 
  return 0;
}
Output:
0

Today we have covered these topics:

C++ Logical Operators – Comparison Operators – The Code Master

Read Our Recent Chapter of C++ : Introduction to C++, C++ Syntax, C++ Output, input, Variables, Data Types, and many more! Follow us on FB

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *