Arithmetic expressions in C++ can be evaluated using arithmetic operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Each operator has precedence which determines in which order operators in an expression are evaluated. Operators with lower-precedence numbers has higher precedence and are evaluated before higher precedence numbers. Parentheses have the highest-precedence, so they can be used to override precedence order. Operators with the same precedence number are evaluated from left to right in the order they appear.
Expression | Explanation |
---|---|
13 + 8 / 4 – 2 * 6 | Initial Expression |
13 + 2 – 2 * 6 | Division is performed first |
13 + 2 – 12 | Multiplication is performed next |
15 – 12 | Addition is performed next |
3 | Subtraction is performed last |
Expression | Explanation |
---|---|
13 + 8 / (4 – 2) * 6 | Initial Expression |
13 + 8 / 2 * 6 | subtraction is performed first |
13 + 4 * 6 | Division is performed next |
13 + 24 | Addition is performed last |
37 | Subtraction is performed last |
Unary operator (one operand) and subtraction is a binary operator (two operands).
Name | Example | Explanation |
---|---|---|
Pre-increment | ++x | Adds 1 to the existing value. Same as: x = x + 1 |
Pre-decrement | --x | Subtracts 1 to the existing value. Same as: x = x - 1 |
Post-increment | x++ | Adds 1 to the existing Same as: x = x + 1 |
Post-decrement | x-- | Subtracts 1 to the existing value. Same as: x = x - 1 |
If prefix form is used then increment or decrement will be done before the rest of the expression, and if postfix form is used then increment or decrement will be done after the complete expression is evaluated.
Expression | Result | Explanation |
---|---|---|
x = --y | x = 9 y = 9 |
The value of y is decremented first before being assigned to x) |
x = y++ + 2 | x = 12 y = 11 |
The value of y is used in the expression and then it is incremented at the end. |
y *= x-- | x = 5 y = 60 |
This is same as y = y * x--. The value of x is used in the expression and it is decremented at the end.) |
An assignment statement is used to assign a value to a variable while a program is running.
Syntax:A assignment operator can be abbreviated as well. Statement must be of the form variableName = variableName arithmeticOperator value, which is abbreviated as variableName arithmeticOperator= value . Most common operators are += , -= , *= , /= , and %=. Table 4.6 shows the expression and abbreviations of it.
Expression | Abbreviated Expression |
---|---|
a = a + b; | a += b; |
a = a - b; | a -= b; |
a = a * b; | a *= b; |
a = a / b; | a /= b; |
a = a % b; | a %= b; |
Relational (also called Comparison operators) are used to compare two values that have the same data type. Table 4.7 shows the relational operators used and its meaning.
Operator | Name | Example | Precedence No. |
---|---|---|---|
< | Less than | height < 180 | 1 |
<= | Less than or equal to | age <= 18 | 1 |
> | Greater than | mark > 50 | 1 |
>= | Greater than or equal to | temp >= 90 | 1 |
== | Equal to | grade == 90 | 2 |
!= | Not equal to | response != 2 | 2 |
Relational expressions are evaluated to a numerical value of 1 or 0 only. If the value is 1, the expression is true. If the value is 0, the expression is false. It is adviseable that not to use equality (==) or inequality operator (!=) to compare real numbers. Real numbers cannot be stored exactly, therefore, test that absolute value of their difference is within some small threshold. char values are automatically coerced to int values for comparison purposes. Strings are compared on a character by character basis. The string with the first lower character is considered smaller
Expression | Explanation |
---|---|
7 – 3 + 8 < 9 + 5 | Initial Expression |
4 + 8 < 9 + 5 | Subtraction is performed first |
12 < 9 + 5 | First addition is performed next |
12 < 14 | Second addition is perfomed next |
true | Less then operator is performed last |
Logical operators allow the combining of two or more conditions (sub-conditions) into one compound condition. Also called Boolean operators and always evaluate to true or false.
The two most common are And (&&) and Or (||). All sub-conditions must be true for a compound condition using And to be true. Only one of the sub-conditions must be true for a compound condition using Or to be true. And is evaluated before Or in an expression.
Logical operators are evaluated after arithmetic and comparison operators. Parentheses can override precedence ordering.
Truth tables summarize how computer evaluates logical operators. Only necessary sub-conditions are evaluated; called short-circuit evaluation . For example: if the first sub-condition in an And clause is false, the second sub-condition need not be evaluated.
Truth Table for And (&&) Operator | ||
---|---|---|
Subcondition1 | Subcondition2 | Subcondition1 && subcondition2 |
True | True | True |
True | False | False |
False | True | False | False | False | False |
Truth Table for Or (||) Operator | ||
---|---|---|
Subcondition1 | Subcondition2 | Subcondition1 && subcondition2 |
True | True | True |
True | False | True |
False | True | True | False | False | False |
Truth Table for Or (||) Operator | ||
---|---|---|
Subcondition | !Subcondition2 | |
True | False | |
False | True |
Operator | Operation | Precedence No. |
---|---|---|
( ) | Parenthesis overrides normal precedence rules | 1 |
! | Negation (reverses the sign of a number) | 2 |
*, /, % | Multiplication, division, and modulus | 3 |
+, - | Addition and subtraction | 4 |
<, <=,>, >= | Less than, less than or equal to, greater than, greater than or equal to | 5 |
==, != | Equal to, not equal to | 6 |
And (&&) | All sub-conditions must be true for the compound condition to evaluate to true | 7 |
Or (||) | Only one sub-condition needs to be true for the compound condition to evaluate to true | 8 |
Expression | Explanation |
---|---|
20 < 80 / 2 + 3 && 25> 10 * 2 | Initial Expression |
20 < 40 + 3 && 25> 10 * 2 | 80 / 2 is performed first |
20 < 40 + 3 && 25> 20 | 10 * 2 is performed next |
20 < 43 && 25> 20 | 40 + 3 is perfomed next |
True && 25 > 20 | 20 < 43 is perfomed next |
True && True | 25 > 20 is perfomed next |
True | True && true evaluates to true |