What factors are used in comparisons? (Comparison Operators) in C++
the name of the employee | his code | Example | Explanation of the code |
---|---|---|---|
Equal to | == | (a == b) | Is a value a equal to a value b ? If the answer is yes, it will return true |
Not equal to | != | (a != b) | Is the value a not equal to the value b ? If the answer is yes, it will return true |
Greater than | > | (a > b) | Is the value a greater than the value b ? If the answer is yes, it will return true |
Less than | < | (a < b) | Is the value a smaller than the value b ?If the answer is yes, it will return true |
Greater than or equal to | >= | (a >= b) | Is the value a greater or equal to the value b ?If the answer is yes, it will return true |
Less than or equal to | <= | (a <= b) | Is the value a smaller or equal to the value b ? If the answer is yes, it will return true |
Explain the factors that are used for comparison in C++
worker ==
(Equal To Operator) in C++
The operator ==
is used to execute a specific code if the value of the first variable is equal to the value of the second variable.
When we put it in the condition, it means is the value of the first variable equal to the value of the second variable?
If it is equal then the answer will be
true
and therefore the code will run.If it is not equal to it, the answer will be
false
, and therefore the code will not be executed.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; int c = 20; // the print command b will be executed with the value of the variable a the following condition means that if the value of the variable is if( a == b ) { cout << "a = b"; } // the print command c will be executed with the value of the variable a the following condition means that if the value of the variable is if( a == c ) { cout << "a = c"; } return 0; }
• We will get the following result when running.
a = b
• We note that he executed the printing command set in the first condition because the answer to the condition was true
.
• The printing command set in the second condition was not executed because the answer to the condition was false
.
worker !=
(Not Equal To Operator) in C++
The operator !=
is used to execute a certain code if the value of the first variable is not equal to the value of the second variable.
When we put it in the condition, it means that the value of the first variable is not equal to the value of the second variable?
If it is not equal to it then the answer will be
true
and therefore the code will run.If it is equal to it, the answer will be
false
, and therefore the code will not be executed.
An example of executing a given code if the value of the first variable is not equal to the value of the second variable.
#include <iostream> using namespace std; int main() { int a = 10; int b = 10; int c = 20; // the print command will be executed b the value of the variable a is not equal to the following condition that means that if the value of the variable is if( a != b ) { cout << "a != b"; } // the print command will be executed c not equal to the value of the variable a The following condition means that if the value of the variable if ( a != c ) { cout << "a != c"; } return 0; }
• We will get the following result when running.
a != c
• We note that the printing command set in the second condition was executed because the answer to the condition was true
.
• The printing command placed in the first condition was not executed because the answer to the condition was false
.
The Worker >
(Greater Than Operator) in C++
The operator >
is used to execute a specific code if the value of the first variable is greater than the value of the second variable.
When we put it in the condition, it means that the value of the first variable is greater than the value of the second variable?
If it is greater than it, the answer will be
true
, and therefore the code will run.If it is not greater than it will be the answer
false
and therefore the code will not run.
An example of executing a given code if the value of the first variable is greater than the value of the second variable.
#include <iostream> using namespace std; int main() { int a = 10; int b = 5; // The print command b will be executed greater than the value of the variable a The following condition means that if the value of the variable if ( a > b ) { cout << "a > b"; } // The print command a is greater than the value of the variable b. The following condition means that if the value of the variable is if ( b > a ) { cout << "b > a"; } return 0; }
• We will get the following result when running.
a > b
• We note that he executed the printing command set in the first condition because the answer to the condition was true
.
• The printing command set in the second condition was not executed because the answer to the condition was false
.
worker <
(Less Than Operator) in C++
The operator <
is used to execute a specific code if the value of the first variable is less than the value of the second variable.
When we put it in the condition, it means is the value of the first variable less than the value of the second variable?
If it is smaller than it will be the answer
true
and therefore the code will run.If it is not smaller than it will be the answer
false
and therefore the code will not execute.
An example of executing a given code if the value of the first variable is less than the value of the second variable.
#include <iostream> using namespace std; int main() { int a = 10; int b = 5; // The print command b will be executed less than the value of the variable a The following condition means that if the value of the variable is if ( a < b ) { cout << "a < b"; } // The print command a will be executed less than the value of the variable b. The following condition means that if the value of the variable is if ( b < a ) { cout << "b < a"; } return 0; }
• We will get the following result when running.
b < a
• We note that the printing command set in the second condition was executed because the answer to the condition was true
.
• The printing command placed in the first condition was not executed because the answer to the condition was false
.
worker >=
(Greater Than or Equal To Operator) in C++
The operator >=
is used to execute a specific code if the value of the first variable is greater than or equal to the value of the second variable.
When we put it in the condition, it means is the value of the first variable greater or equal to the value of the second variable?
If it is greater than or equal to it, the answer will be
true
, and therefore the code will be executed.If it is not greater than or equal to it, the answer will be
false
, and therefore the code will not be executed.
Example of executing a given code if the value of the first variable is greater than or equal to the value of the second variable
#include <iostream> using namespace std; int main() { int a = 5; int b = 5; int c = 10; // The print command b will be executed greater than or equal to the value of the variable a The following condition means that if the value of the variable if( a >= b ) { cout << "a >= b" << endl; } // The print command c will be executed as greater than or equal to the value of the variable a The following condition means that if the value of the variable is if( a >= c ) { cout << "a >= c" << endl; } // The print command a is greater than or equal to the value of the variable c. The following condition means that if the value of the variable is if( c >= a ) { cout << "c >= a" << endl; } return 0; }
• We will get the following result when running.
a >= b c >= a
• We note that the printing command set in the first and third conditions has been executed because the answer to the condition was true
.
• The printing command set in the second condition was not executed because the answer to the condition was false
.
worker <=
(Less Than or Equal To Operator) in C++
The operator <=
is used to execute certain code if the value of the first variable is less than or equal to the value of the second variable.
When we put it in the condition, it means is the value of the first variable less than or equal to the value of the second variable?
If it is less than or equal to it, the answer will be
true
, and therefore the code will run.If it is not less than or equal to it, the answer will be
false
, and therefore the code will not be executed.
An example of executing a given code if the value of the first variable is less than or equal to the value of the second variable.
#include <iostream> using namespace std; int main() { int a = 5; int b = 5; int c = 10; // The print command b will be executed less than or equal to the value of the variable a The following condition means that if the value of the variable if( a <= b ) { cout << "a <= b" << endl; } // The print command c will be executed less than or equal to the value of the variable a The following condition means that if the value of the variable if ( a <= c ) { cout << "a <= c" << endl; } // The print command a will be executed less than or equal to the value of the variable c The following condition means that if the value of the variable is if( c <= a ) { cout << "c <= a" << endl; } return 0; }
• We will get the following result when running.
a <= b a <= c
• We note that the printing command set in the first and second conditions has been executed because the answer to the condition was true
.
• The printing order placed in the third condition was not executed because the answer to the condition was false
.