You are on page 1of 23

C++ Relational Operators

OBJECTIVES
After studying this unit, students would be able
to appreciate the following:
1. Fundamental Concepts of C++ Relational
Operators
2. Identify the Structure and basic features of
C++ Syntax
3. Demonstrate basic coding skills in C++
1
MESSAGE OF THE DAY

They that sow in tears shall


reap in joy.

Psalm: 126:5

2
Relational Operators

• C++ Relational Operators are used to relate or


compare given operands.
• Relational operations are like checking if two
operands are equal or not equal, greater or
lesser, etc.
• Relational Operators are also called
Comparison Operators.

3
Relational Operators

• The syntax of any Relational Operator with


operands is
operand1 operator_symbol operand2

For example, to check if x and y are equal, we


use the following expression.
x == y

4
C++ Relational Operators

In this lesson, we will learn about different


Relational Operators available in C++
programming language and go through each of
these Relational Operations in detail, with the
help of examples.

The following table specifies symbol, example,


and description for each of the Assignment
Operator in C++.

5
C++ Relational Operators

6
C++ Relational Operators

Since Relational Operators return boolean value,


they can be used as conditions in Conditional
Statements.
We can compare any two values or objects, but
these values or objects must be comparable by
C++.

7
Equal to

• In the following example, we take two values


in x and y, and programmatically check if x
equals y using Equal to Operator.
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 5;

if (x == y) {
cout << "x and y are equal." << endl;
} else {
cout << "x and y are not equal." << endl;
}
}
8
Not Equal

• In the following example, we will take two


values in x and y, and programmatically check
if x does not equal y using Not Equal Operator.
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 4;

if (x != y) {
cout << "x and y are not equal." << endl;
} else {
cout << "x and y are equal." << endl;
}
}
9
Greater than
• In the following example, let us consider two
values in x and y, and programmatically check
if x is greater than y using Greater than
Operator. #include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 4;

if (x > y) {
cout << "x is greater than y." << endl;
} else {
cout << "x is not greater than y." << endl;
}
} 10
Less than
• In the following example, let us consider two
values in x and y, and programmatically check
if x is less than y using Less than Operator.
#include <iostream>
using namespace std;

int main() {
int x = 2;
int y = 4;

if (x < y) {
cout << "x is less than y." << endl;
} else {
cout << "x is not less than y." << endl;
}
}
11
Greater than or equal to
• In the following example, le us take two values
in x and y, and programmatically check if x is
greater than or equal to y using Greater than
or equal to Operator.
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 4;

if (x >= y) {
cout << "x is greater than or equal to y." << endl;
} else {
cout << "x is not greater than or equal to y." << endl;
}
}
12
Less than or equal to
• In the following example, let us take two
values in x and y, and programmatically check
if x is less than or equal to y using Less than or
equal to Operator.
#include <iostream>
using namespace std;

int main() {
int x = 2;
int y = 4;

if (x <= y) {
cout << "x is less than or equal to y." << endl;
} else {
cout << "x is not less than or equal to y." << endl;
}
}
13
Conclusion

• In this lesson, we learned what Relational


Operators are, and how to use them in C++
programs, with the help of examples.

14
C++ Comments
•Comments in a C++ program are not
compiled and ignored during execution.
Comments are used to increase the
readability of the program.
•There are two types of comments in C++
based on number of lines the comment is.
They are:
• Single Line Comments
• Multiple Line Comments
15
C++ Comments
•In this lesson, we shall learn how to write
single line comments and multiple line
comments.
Single Line C++ Comments
•To write single line comments in C++
program, use double forward slash //.
•Anything after // in that line is considered a
comment.

16
Example
•In the following example, let us write a C++
program, with single line comments.
#include <iostream>
using namespace std;

int main() {
//this is a comment
cout << "Hello World!"; //another comment
Return 0;

17
Multiple Line C++ Comments
•You can also write comments that span over
two or more lines.
•To write multiple line comments, start your
comment with /* and end with */.
Example
•Following is an example program, where we
have a multiple line comment enclosed
between /* and */.

18
Multiple Line C++ Comments
. #include <iostream>
using namespace std;

int main() {
/*
This is a
multiple line
comment
*/
cout << "Hello World!";
Return 0;
}

19
QUESTIONS TIME

20
NEXT TOPIC

• If Else Statements

21
Reading Assignment
• Control Structures

22
Reference
1. https://www.tutorialkart.com/cpp/cpp-
relational-operators/

23

You might also like