C++ Programming
Relational operators[edit]
The operators < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), and != (not equal to) are relational operators that are used to compare two values. Variables may be compared to another variable or to a literal.
The < operator checks if the first operand is less than the second operand. If the first operand is less than the second operand, returns true. Else returns false.
- Examples
int x =5;
int y = 1;
if (x < 10) //x is 5 which is less than 10, will return true
{
//...code...
}
if (x < 0) //x is 5 which is not less than 0, will return false
{
//...code...
}
if (x < y) //x is 5 and y is 1. 5 is not less than 1, will return false
{
//...code...
}
The > operator checks if the first operand is greater than the second operand. If the first operand is greater than the second operand, returns true. Else returns false.
- Examples
int x =12;
int y = 1;
if (x > 10) //x is 12 which is greater than 10, will return true
{
//...code...
}
if (x > 15) //x is 12 which is not greater than 15, will return false
{
//...code...
}
if (x > y) //x is 12 and y is 1. 12 is greater than 1, will return true
{
//...code...
}
The <= operator checks if the first operand is less than or equal to the second operand. If the first operand is less than or equal to the second operand, returns true. Else returns false.
- Examples
int x = 12;
int y = 12;
if (x <= 12) //x is 12 which is less than or equal to 12, will return true
{
//...code...
}
if (x <= 5) //x is 12 which is not less than or equal to 5, will return false
{
//...code...
}
if(x <= y) //x is 12 and y is 12. 12 is less than or equal to 12, will return true
{
//...code...
}
The >= operator checks if the first operand is greater than or equal to the second operand. If the first operand is greater than or equal to the second operand, returns true. Else returns false.
- Examples
int x = 12;
int y = 12;
if (x >= 12) //x is 12 which is greater than or equal to 12, will return true
{
//...code...
}
if (x >= 19) //x is 12 which is not greater than or equal to 19, will return false
{
//...code...
}
if (x >= y) //x is 12 and y is 12. 12 is greater than or equal to 12, will return true
{
//...code...
}
The == operator checks if the first operand is equal to the second operand. If the first operand is equal to the second operand, returns true. Else returns false.
- Examples
int x = 5;
int y = 6;
if (x == 5) //x is 5 which is equal to 5, returns true
{
//...code...
}
if (x == 7) //x is 5 which is not equal to 7, returns false
{
//...code...
}
if (x == y) //x is 5 and y is 6. 5 is not equal to 6, returns false
{
//...code...
}
The != operator checks if the first operand is not equal to the second operand. If the first operand is not equal to the second operand, returns true. Else returns false.
- Examples
int x = 5;
int y = 6;
if (x != 5) //x is 5 which is equal to 5, returns false
{
//...code...
}
if (x != 7) //x is 5 which is not equal to 7, returns true
{
//...code...
}
if (x != y) //x is 5 and y is 6. 5 is not equal to 6, returns true
{
//...code...
}
Logical operators[edit]
The operators and (can also be written as &&) and or (can also be written as ||) allow two or more conditions to be chained together. The and operator checks whether all conditions are true and the or operator checks whether at least one of the conditions is true. Both operators can also be mixed together in which case the order in which they appear from left to right, determines how the checks are performed. Older versions of the C++ standard used the keywords && and || in place of and and or. Both operators are said to short circuit. If a previous and condition is false, later conditions are not checked. If a previous or condition is true later conditions are not checked.
The not (can also be written as !) operator is used to return the inverse of one or more conditions.
- Syntax:
condition1 and condition2 condition1 or condition2 not condition
- Examples:
When something should not be true. It is often combined with other conditions. If x>5 but not x = 10, it would be written:
if ((x > 5) and not (x == 10)) // if (x greater than 5) and ( not (x equal to 10) )
{
//...code...
}
When all conditions must be true. If x must be between 10 and 20:
if (x > 10 and x < 20) // if x greater than 10 and x less than 20
{
//....code...
}
When at least one of the conditions must be true. If x must be equal to 5 or equal to 10 or less than 2:
if (x == 5 or x == 10 or x < 2) // if x equal to 5 or x equal to 10 or x less than 2
{
//...code...
}
When at least one of a group of conditions must be true. If x must be between 10 and 20 or between 30 and 40.
if ((x >= 10 and x <= 20) or (x >= 30 and x <= 40)) // >= -> greater or equal etc...
{
//...code...
}
Things get a bit more tricky with more conditions. The trick is to make sure the parenthesis are in the right places to establish the order of thinking intended. However, when things get this complex, it can often be easier to split up the logic into nested if statements, or put them into bool variables, but it is still useful to be able to do things in complex boolean logic.
Parenthesis around x > 10 and around x < 20 are implied, as the < operator has a higher precedence than and. First x is compared to 10. If x is greater than 10, x is compared to 20, and if x is also less than 20, the code is executed.
and (&&)[edit]
statement1 | statement2 | and |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | F |
The logical AND operator, and, compares the left value and the right value. If both statement1 and statement2 are true, then the expression returns TRUE. Otherwise, it returns FALSE.
if ((var1 > var2) and (var2 > var3))
{
std::cout << var1 " is bigger than " << var2 << " and " << var3 << std::endl;
}
In this snippet, the if statement checks to see if var1 is greater than var2. Then, it checks if var2 is greater than var3. If it is, it proceeds by telling us that var1 is bigger than both var2 and var3.
or (||)[edit]
statement1 | statement2 | or |
---|---|---|
T | T | T |
T | F | T |
F | T | T |
F | F | F |
The logical OR operator is represented with or. Like the logical AND operator, it compares statement1 and statement2. If either statement1 or statement2 are true, then the expression is true. The expression is also true if both of the statements are true.
if ((var1 > var2) or (var1 > var3))
{
std::cout << var1 " is either bigger than " << var2 << " or " << var3 << std::endl;
}
Let's take a look at the previous expression with an OR operator. If var1 is bigger than either var2 or var3 or both of them, the statements in the if expression are executed. Otherwise, the program proceeds with the rest of the code.
not (!)[edit]
The logical NOT operator, not, returns TRUE if the statement being compared is not true. Be careful when you're using the NOT operator, as well as any logical operator.
not x > 10
The logical expressions have a higher precedence than normal operators. Therefore, it compares whether "not x" is greater than 10. However, this statement always returns false, no matter what "x" is. That's because the logical expressions only return boolean values(1 and 0).