Variables\Assignments : 2.4 Arithmetic expressions (general)
precedence rules
An expression is evaluated using the order of standard mathematics
Select the expression whose parentheses match the evaluation order of the original expression. x * y * z ?
(x * y ) * z The two operators have equal precedence, so evaluation occurs left-to-right.
Select the expression whose parentheses match the evaluation order of the original expression. x / 2 + y / 2
(x / 2) + (y / 2) / has precedence over +. The two / are evaluated left-to-right.
Select the expression whose parentheses match the evaluation order of the original expression. z / 2-x ?
(z / 2) - x / has precedence over -.
Which operator is evaluated second?
* Multiply and divide operations have precedence over add and subtract
Which operator is evaluated first?
+ Parentheses have the highest precedence, so + is evaluated first.
Which operator is evaluated third?
- Lastly, - is evaluated.
What is totCount after executing the following? numItems = 5 totCount = 1 + (2 * numItems) * 4 ?
41 After (2 * 5) is evaluated, * 4 is evaluated because * has precedence over +.
Indicate which are valid expressions. x and y are variables: 2x ?
False - In programming, multiplication typically must be indicated explicitly using the * operator
Indicate which are valid expressions. x and y are variables: 2 + (xy) ?
False - In programming, omitting the multiplication sign, as in xy, is not usually allowed, because xy could be the name of another variable.
Does the expression correctly capture the intended behavior? n factorial n! ?
False - Most languages don't use the ! symbol for factorial.
Indicate which are valid expressions. x and y are variables: y = x + 1 ?
False - x + 1 alone is an expression, but y = x + 1 is an assignment and not itself an expression.
+ - (fourth) (Precedence rules for arithmetic operators)
Finally come + and - with equal precedence. In y = 3 + 2 * x, the 2 * x is evaluated first, with the result then added to 3, because * has higher precedence than +. Spacing doesn't matter: y = 3+2 * x would still evaluate 2 * x first.
Indicate which are valid expressions. x and y are variables: 2 ?
True - An expression can just be a literal.
Indicate which are valid expressions. x and y are variables: x ?
True - An expression can just be a variable.
Indicate which are valid expressions. x and y are variables: x + 1 ?
True - The text consists of a variable (x), operator (+), and literal (1). If x is 5, the expression evaluates to 5 + 1 or 6.
Does the expression correctly capture the intended behavior? The negative of userVal: --userVal ?
True - serves as subtraction, but also negation.
Select the expression whose parentheses match the evaluation order of the original expression. y + 2 * z ?
Y + (2 * z) * has precedence over +, so * is evaluated first.
literal
a specific value in code, like 2
addition - (Common Arithmetic operators)
is +, as in x + y.
expression
is a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value. Ex: 2 * (x + 1) is an expression. If x is 3, the expression evaluates to the value 8. Expressions are commonly used on the right side of an assignment statement, as in y = 2 * (x + 1).
operator
is a symbol that performs a built-in calculation, like the operator + which performs addition.
multiplication - (Common Arithmetic operators)
operator is *, as in x * y.
subtraction - (Common Arithmetic operators)
operator is -, as in x - y. Also, the - operator is for negation, as in -x + y, or x + -y.
division - (Common Arithmetic operators)
operator is /, as in x / y.
unary - (second) (Precedence rules for arithmetic operators)
used for negation (unary minus) is next In 2 * -x, the -x is computed first, with the result then multiplied by 2.
evaluates
value, which replaces the expression. Ex: If x is 5, then x + 1 evaluates to 6, and y = x + 1 assigns y with 6.
Select the expression whose parentheses match the evaluation order of the original expression. x + 1 * y/2 ?
x + ((1 * y)) / 2) * and / have precedence over +, so will be evaluated first. * and / have equal precedence, so are evaluated left-to-right, despite what the original spacing implied.
left-to-right (last) - (Precedence rules for arithmetic operators)
If more than one operator of equal precedence could be evaluated, evaluation occurs left to right. In y = x * 2 / 3, the x * 2 is first evaluated, with the result then divided by 3.
[ ]
In mathematics, subexpressions may be enclosed in parentheses ( ), by brackets [ ] as above, or even by braces { }, to improve readability. In programming, typically only parentheses are allowed. Brackets and braces are typically used for other purposes in programs.
spaces in variable names
Informally, quantities may be represented with multiple words, as in Heart Rate. In a program, a variable's name must be one word, as in heartRate. Above, the programmer used heartBPM to indicate beats-per-minute for the rate.
( ) - (first) (Precedence rules for arithmetic operators)
Items within parentheses are evaluated first In 2 * (x + 1), the x + 1 is evaluated first, with the result then multiplied by 2.
* / - (third) (Precedence rules for arithmetic operators)
Next to be evaluated are * and /, having equal precedence.
