Coder Social home page Coder Social logo

programming-univbasics-the-constant-expression-austin-web-030920's Introduction

The Constant Expression

Learning Goals

  • Identify the constant expression
  • Explain how the constant expression stops evaluation

Introduction

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.

IRB Constant Expression

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.

Explain How the Constant Expression Stops Evaluation

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:

Math Expression: Step 0

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."

Math Expression: Step 1

Eventually it reaches 4 and that's plain old data. It then checks the other side and sees 10, also plain old data.

Math Expression: Step 1.5

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:

Math Expression: Step 2

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"):

Math Expression: Step 3

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.

Table Explanation

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!

Conclusion

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.

programming-univbasics-the-constant-expression-austin-web-030920's People

Contributors

maxwellbenton avatar sgharms avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.