C++ Relational Operators
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
Psalm: 126:5
2
Relational Operators
3
Relational Operators
4
C++ Relational Operators
5
C++ Relational Operators
6
C++ Relational Operators
7
Equal to
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
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
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