|
What Is an Expression? |
Learn what expressions are in Sheerpower, including numbers, text, calculations, variables, functions, comparisons, and table-field expressions such as employee(annual_salary)/12.
An expression is any piece of code that Sheerpower can work out to a single answer.
When you write:
Sheerpower works out 2 + 3 and gets one answer:
5.
That means 2 + 3 is an expression.
When you write:
Sheerpower prints two expressions on the same line. The first expression
is "Good ". The second expression is
"morning".
Together, the output is:
Even a single number, a single string, or a single table field can be an expression, because Sheerpower already knows its value.
The simplest way to recognize an expression is to ask:
Can Sheerpower turn this into one answer?
If the answer is yes, then it is an expression.
Expressions are used anywhere a program needs a value.
A value might be:
Each of these is an expression:
Each line gives Sheerpower something it can evaluate to one answer.
A numeric expression produces a number.
Sheerpower evaluates each expression before printing the result.
The expression base_price + tax is worked out to one
numeric value.
A text expression produces a string.
Text expressions are commonly used to build messages and labels.
Each item separated by a semi-colon is an expression. Sheerpower evaluates each expression and prints the results together.
A print statement can print one expression or several
expressions. When printing several expressions, Sheerpower best practice
is to separate them with semi-colons.
In the last example, the print statement prints two
expressions. The first expression is the label
"Monthly salary: ". The second expression is
employee(annual_salary) / 12, which takes the employee's
annual salary and divides it by 12.
Problem: It is easy to think that text output should be built by joining strings together before printing them.
Solution: In Sheerpower, when printing multiple values, separate each expression with a semi-colon.
Efficiency: This avoids unnecessary string concatenation and makes each printed expression easier to see.
Takeaway: Use concatenation when you need to create a new string value. Use semi-colons when you simply want to print several expressions together.
A variable by itself is an expression because Sheerpower can look up its current value.
In the last line, quantity * price is also an expression.
Sheerpower gets the value of each variable, multiplies them, and
produces one answer.
In Sheerpower, a table field can be used directly in an expression using the table-name and field-name syntax:
This means the salary field in the current
employee record.
A table field by itself is an expression:
Each one produces a single value from the current employee record.
Table fields become especially useful when they are combined with other values.
Each of these is an expression:
employee(salary) + 100employee(annual_salary) / 12employee(salary) * 12employee(hours) * employee(rate)Sheerpower evaluates the table fields, performs the calculation, and produces one answer.
Expressions are used constantly in business programs.
These expressions represent common business values:
An expression can be evaluated and assigned to a variable.
The right side of each assignment is an expression. Sheerpower evaluates it first, then stores the result.
The print statement can print expressions directly.
The expression does not need to be stored in a variable first. If Sheerpower can evaluate it, it can be printed.
An expression can also produce a true-or-false answer.
The expression:
produces one answer: true or false.
That makes it a Boolean expression.
Boolean expressions are often used with if statements.
Each condition is an expression that Sheerpower can evaluate to true or false.
Expressions can compare one table field to another.
Each comparison is a single expression.
A print statement can display literal text and table fields
together by separating each expression with a semi-colon.
This is useful for labels, reports, messages, logs, and screen output.
Sometimes you really do want to create a new string value. In that case, string concatenation is appropriate.
The important distinction is this:
Reports often use expressions to calculate and display values.
The report does not need a separate field for every value. Some values can be calculated as expressions when needed.
Sheerpower has many built-in functions that can be used directly in expressions. A function receives one or more values, does some useful work, and returns one answer.
Because a function returns one answer, the function call itself is an expression.
Built-in functions can also be combined with table fields and ordinary calculations.
String functions can also be used directly in expressions.
These function calls can be printed, assigned, compared, or used inside larger expressions.
For more examples of math-related built-in functions, see Common Math Functions.
For more examples of built-in string functions, see String Manipulation Functions and Features.
For string searching functions and techniques, see String Comparing and Searching.
Expressions can contain smaller expressions inside larger expressions.
Sheerpower works from the inside outward until the entire expression has one final value.
Parentheses can make an expression easier to read.
These two expressions may not mean the same thing.
The first expression divides only employee(bonus) by 12.
The second expression adds salary and bonus first, then divides the
total by 12.
When the business meaning matters, use parentheses to make the expression obvious.
Expressions are also used when updating values.
In each assignment, the expression on the right is evaluated first. The result is then stored into the field on the left.
Business applications often use expressions involving money.
Each expression produces one money-related value.
Problem: Business programs constantly need calculated values, such as totals, balances, payments, salaries, taxes, and discounts.
Solution: Expressions let those values be written directly where they are needed.
Efficiency: The program does not need extra temporary fields for every small calculation.
Takeaway: Expressions are how Sheerpower turns business rules into working values.
Percentages are also expressions.
A raise, tax, discount, or interest charge can all be calculated with an expression.
Expressions are not limited to numbers. They can also create text values.
The assignment creates one new string value. The print
statements display several expressions by separating them with
semi-colons.
Date values can also be used in expressions.
Depending on the business rule, date fields can be displayed, compared, or used in calculations.
The comparison is an expression that produces true or false.
Expressions are often used inside loops.
The expression:
is evaluated for each employee record.
A report line often contains several expressions.
Each item being printed is an expression.
Here are some common expression patterns.
These are all expressions:
Each one can be evaluated to a single answer.
A statement tells Sheerpower to do something.
An expression gives Sheerpower a value to work out.
In this example:
print is the statement."Monthly salary: " is the first expression.
employee(annual_salary) / 12 is the second expression.
The statement says what to do. The expressions supply the values.
This example uses several expressions to print a simple employee salary summary.
This example includes:
printifExpressions are the building blocks of programs.
Whenever your program needs a value, you give Sheerpower an expression. Sheerpower evaluates that expression and produces the answer.
That answer might be printed, assigned, compared, stored, or used in a larger calculation.
In each case, expressions provide the values that the program works with.
Summary: An expression is any piece of code that Sheerpower can evaluate to one answer. Expressions can be numbers, strings, variables, table fields, calculations, comparisons, or built-in function calls.
The table-field form tablename(fieldname) is especially
important in business programs because it lets you use the current
record's data directly in calculations, reports, comparisons, and
updates.
Built-in functions are also expressions when they return a value. They
can be used directly with numbers, strings, table fields, calculations,
comparisons, assignments, and print statements.
When printing several expressions, separate them with semi-colons. Use concatenation only when you need to create one new string value.
42, "hello", or
employee(salary) is an expression.
employee(salary) + 100 are
expressions.
employee(annual_salary) / 12.
employee(salary) > 100000 are
Boolean expressions.
print statements,
assignments, conditions, reports, and field updates.
tablename(fieldname) syntax makes table data easy
to use directly in business calculations.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |