4.1. ExpressionsThere are several building blocks of coding that you need to understand: statements, expressions, and operators.
An operator is a code element that acts on an expression in some way. For instance, a minus sign can be used to tell the computer to decrement the value of the expression after it from the expression before it. The most important thing to understand about expressions is how to combine them into compound expressions and statements using operators. So we're going to look at operators used to turn expressions into more complex expressions and statements. The simplest form of expression is a literal or a variable. A literal evaluates to itself. Some examples of literals
Although a literal or variable may be a valid expression, they aren't expressions that do anything. You get expressions to do things such as math or assignment by linking them together with operators.
An operator combines simple expressions into more complex expressions by creating relationships between simple expressions that can be evaluated. For instance, if the relation you want to establish is the cumulative joining of two numeric values together, you could write "3 + 4". Figure 4-1 shows how the parts of an expression come together. Figure 4-1. Operands and operators working together as an expression to form a valueThe numbers 3 and 4 are each valid expressions. The equation 3 + 4 is also a valid expression, whose value, in this case, happens to be 7. The plus sign (+) is an operator. The numbers to either side of it are its arguments, You've probably guessed from this information that two or more expressions connected by operators are called an expression. You're right, as operators create complex expressions. The more subexpressions and operators, the longer and more complex the expression. But no matter what, as long as it equates to a value, it's still an expression. When expressions and operators are assembled to produce a piece of code that actually does something, you have a statement. We discussed statements in Chapter 3. They end in semicolons, which is the programming equivalent of a complete sentence. For instance, $Margaritaville + $Sun_Tan_Application is an expression. It equates to something, which is the sum of the values of $Margaritaville + $Sun_Tan_Application, but it doesn't do anything. While it's an expression, the output doesn't make any sense, but if you add the equals sign (=), $Fun_in_the_Sun = $Margaritaville + $Sun_Tan_Application;, you get a statement, because it does something. As Example 4-1 demonstrates, it assigns the sum of the values of $Margaritaville + $Sun_Tan_Application to $Fun_in_the_Sun. Example 4-1. Sum of values
Example 4-1 outputs:
There really isn't much more to understand about expressions except for the assembly of them into compound expressions and statements using operators. Next, we're going to discuss operators that are used to turn expressions into more complex expressions and statements. |
Friday, October 23, 2009
Section 4.1. Expressions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment