- Identify the constant expression
- Explain how the constant expression stops evaluation
Lets repeat our definition of expression
Definition: Expression: A combination of information called data and symbols indicating how to combine data called operators.
What if we were to make an expression that had no operators? What if it only had data. Here we're giving just such a thing to Ruby in IRB.
This expression is the constant expression and it's very important, although very boring.
It's boring because it doesn't do anything except be itself. But it's important because it confirms that Ruby knows when to stop applying operations. It tells Ruby to stop, you have an answer.
Let's consider a simple arithmetic expression. Keep in mind we apply operators in "PEMDAS" order: parenthesis, exponents, multiplication, division, addition, subtraction.
We'll start with the expression:
Ruby's mission is to find a constant piece of data or a constant expression.
Because of ()
, it goes there first. The (10 - 4)
is clearly not a
constant expression because of the -
operator's presence. Ruby makes a "tree"
of the two sides of the operator (-
) and then looks on each side to see
whether those sides are constant expressions i.e. "plain old data."
Eventually it reaches 4
and that's plain old data. It then checks the other
side and sees 10
, also plain old data.
Since these two are constant expressions, it can apply -
to them and produce
6
โ a constant expression.
So what Ruby now sees looks like this:
By the same logic of what we saw inside the ()
, Ruby then applies the *
to
3
and 6
and creates a new expression, a constant expression the answer
(or "return value"):
Whew! Fortunately, Ruby does all this work of building a tree of operators and returning a value very quickly!
The constant expression is always the last expression in a complex expression. It's how Ruby knows it has data that it can work with and that no other operations need to be applied.
Another way of looking at this process might be to look at a table. We'll repeat all the same things we just showed graphically, but if a table makes more sense for you, then you'll like this one better!
This is an important tool when learning to program, if you like thinking in code, try out the code; if you prefer diagrams, draw the diagram. An important part of learning to be a technologist is learning to build the technology you need to learn more technology.
Expression | Has Operators? | Operators | Are we done? | Which to Apply |
---|---|---|---|---|
3*(10-4) |
YES | * , () |
NO | Zoom-in on new sub-expression in () because of PEMDAS |
(10-4) |
YES | - |
NO | Evaluate sub-expressions |
10 |
NO | NONE | YES | Zoom in on expression 10 . Constant expression! Return the value of the constant, we're done! |
4 |
NO | NONE | YES | Zoom in on expression 4 . Constant expression! Return the value of the constant, we're done! |
(10-4) |
YES | - |
NO | Replace ( 10 - 4 ) with application of - to 10 and 4 making 6 |
3*6 |
YES | * |
NO | Zoom-out and replace the sub-expression with its value we just determined |
3*6 |
YES | - |
NO | Zoom-in on sub-expression |
3 |
NO | NONE | YES | Zoom in on expression 3 . Constant expression! Return the value of the constant, we're done! |
6 |
NO | NONE | YES | Zoom in on expression 6 . Constant expression! Return the value of the constant, we're done! |
3*6 |
YES | * |
NO | Replace 3 * 6 with application of * to 3 and 6 making 18 |
18 |
NO | NONE | YES | Constant expression! Return the value of the constant, we're done! |
While the constant expression might seem dull, it lets us know when
expression evaluation is done and establishes a groundwork for all the
following expressions. The first rule of Aristotle's logic is A is A
; the
constant expression provides a similar "foundation" for programming.