Send
Close Add comments:
(status displays here)
Got it! This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. Note: This appears on each machine/browser from which this site is accessed.
Python: Expressions
1. Lua: Expressions
2. Expressions
3. Facial expressions
An expression is a formula that evaluates to and can be expressed as a literal value.
The word expression, from the Latin, literally means the "pressing out" (out being "ex").
A facial expression, such as a smile or frown, is pressed out from the face.
A computer code expression is a formula whose value is "pressed out" (or reduced) to a literal value.
4. Formulas and expressions
Be aware that mathematicians often call an expression a
formula while computer scientists often call a formula an
expression.
Does a literal express a value? Yes. A literal is an expression.
Does a variable express a value? Yes. A variable is an expression.
Is 3 + 4 an expression? Yes. A formula is an expression. This one evaluates to 7.
5. Integer arithmetic operations
Integer arithmetic operators |
Operator |
Operation |
Expression |
Value |
+ |
addition |
4 + 3 |
7 |
- |
subtraction |
4 - 3 |
1 |
* |
multiplication |
4 * 2 |
12 |
/ |
division |
4 / 3 |
1 |
% |
modulus |
4 % 3 |
1 |
Note: Floating point operations have no modulus and division yields a number with a fractional part.
Expression evaluation |
Expression |
Value |
156 / 16 |
9 |
156 % 16 |
12 |
3 + 4 * 5 |
23 |
3 + (4 * 5) |
23 |
(3 + 4) * 5 |
60 |
60 / 6 * 5 |
50 |
(60 / 6) * 5 |
50 |
Here are the expressions that we evaluated in converting the number
19 from decimal to binary.
Quotient , Modulus/Remainder
19 / 2 => , 9 19 % 2 => 1
9 / 2 => 4 , 9 % 2 => 1
4 / 2 => 2 , 4 % 2 => 0
2 / 2 => 1 , 2 % 2 => 0
1 / 2 => 0 , 1 % 2 => 1
6. Floating point numbers
A floating point number is an approximation of a real number.
What is 1.0/3.0?
The answer in math class is 0.3333333....
Mathematicians are rarely interested in efficiency. In computer terms, we do not have an infinite number of bits, and we need the division operation to compute fast, so we need to approximate the value. In such cases, a floating point approximation is used.
A double is a C/C++ floating point number. There is also a float type with less precision. Unless told to do so, always use double and not float.
7. Recursive
Something is
recursive if it directly or indirectly refers to itself.
Is no your answer to this question?
The next sentence is true. The previous sentence is false.
Problems sometimes result if the recursive relationship is circular.
A procedure or function is recursive if the procedure and/or function calls itself either directly or indirectly.
Remember the message passing model.
8. Expressions: recursive definition
An
expression is a formula that evaluates to a value. That is, an expression expresses a value.
All literals are expressions.
All constants are expressions.
All variables are expressions.
If E1 and E2 are expressions, then so is E1 op2 E2 where op2 is a binary operator.
If E1 is an expression, then so is op1 E1 where op1 is a unary operator.
If E is an expression, then so is (E).
A function call is an expression since the value returned by the function can be used in the expression.
The recursive definition of expressions provides a simple and, yet, powerful way to recognize and evaluate expressions.
9. Parentheses
Consider the expression
(3 + 4) * 5 that evaluates to
35. Here is the tree diagram for this expression.
Mathematicians think in terms of parentheses. Computer scientists write parentheses in programs, but think in terms of tree structures. One works up from the leaves to the root to determine the value of the expression.
10. Parentheses
Let us change the parentheses and use the expression
3 + (4 * 5) which evaluates to
23. Here is the tree diagram for this expression.
Notice that tree structures have no parentheses. To a computer scientist, a program is just a big tree structure to be evaluated (and executed).
11. Precedence rule
The precedence rule for 3 + 4 * 5 is to do multiplication first, as in 3 + (4 * 5) instead of addition first as in (3 + 4) * 5.
Whenever one is in doubt, just use parentheses to get the desired expression evaluation.
12. End of page