0% found this document useful (0 votes)
12 views6 pages

C++ Lec6-Operaters2

The document discusses comparison and logical operators in C++. It lists six comparison operators (==, !=, >, <, >=, <=) and provides examples of how each is used to compare two values. It also discusses the three logical operators (&&, ||, !) and provides their names, descriptions, and examples of how they are used to determine logical relationships between variables or values. The document provides code examples for each operator.

Uploaded by

Rasty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

C++ Lec6-Operaters2

The document discusses comparison and logical operators in C++. It lists six comparison operators (==, !=, >, <, >=, <=) and provides examples of how each is used to compare two values. It also discusses the three logical operators (&&, ||, !) and provides their names, descriptions, and examples of how they are used to determine logical relationships between variables or values. The document provides code examples for each operator.

Uploaded by

Rasty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Comparison Operators

Comparison operators are used to compare two values.

Note: The return value of a comparison is either true (1) or false (0).

In the following example, we use the greater than operator (>) to find out if 5 is greater than 3:

A list of all comparison operators:

Operator Name Example


== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
Greater than or
>= x >= y
equal to
Less than or
<= x <= y
equal to

Example of Equal to:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x == y); // returns 0 (false) because 5 is not equal to 3
return 0;
}

Programming C++ Samir Bilal Practical & Theoretical


2

Example of Not Equal:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x != y); // returns 1 (true) because 5 is not equal to 3
return 0;
}

Example of Greater than:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
return 0;
}

Example of Less than:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x < y); // returns 0 (false) because 5 is not less than 3
return 0;}
Programming C++ Samir Bilal Practical & Theoretical
3

Example of Greater than or equal to:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x >= y); // returns 1 (true) because five is greater than, or equal, to 3
return 0;
}

Example of Less than or equal to:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x <= y); // returns 0 (false) because 5 is neither less than or equal to 3
return 0;
}

Programming C++ Samir Bilal Practical & Theoretical


4

Logical Operators

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example

&& Logical and Returns true if both statements are true x < 5 && x < 10

|| Logical or Returns true if one of the statements is true x < 5 || x < 4


! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)

&& ||
!

Programming C++ Samir Bilal Practical & Theoretical


5

Example of Logical and:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10); // returns true (1) because 5 is greater than 3 AND 5 is less than 10
return 0;
}

Example of Logical or:

#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 || x < 4); // returns true (1) because one of the conditions are true (5 is greater
than 3, but 5 is not less than 4)
return 0;
}

Example of Logical not:

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (!(x > 3 && x < 10)); // returns false (0) because ! (not) is used to reverse the result
return 0; }
Programming C++ Samir Bilal Practical & Theoretical
6

Q1) How many comparison operators we have list six of them and write the name with
example?

Q2) Define logical operators & how many logical operators we have list them with( name,
description and example)
Q3) What are the truth table of (and, or , not)?

Programming C++ Samir Bilal Practical & Theoretical

You might also like