Popup YouTube Video
Sheerpower Logo

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.

What Is an Expression?

An expression is any piece of code that Sheerpower can work out to a single answer.

When you write:

print 2 + 3

Sheerpower works out 2 + 3 and gets one answer: 5.

That means 2 + 3 is an expression.

When you write:

print "Good "; "morning"

Sheerpower prints two expressions on the same line. The first expression is "Good ". The second expression is "morning".

Together, the output is:

Good morning

Even a single number, a single string, or a single table field can be an expression, because Sheerpower already knows its value.

print 42 print "hello" print employee(salary)

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 Produce Values

Expressions are used anywhere a program needs a value.

A value might be:

  • a number to print
  • a name to display
  • a price to calculate
  • a salary to update
  • a true-or-false answer to test

Each of these is an expression:

print 100 print 25 + 75 print "Employee name: "; employee(name) print employee(salary) + 100 print employee(active?)

Each line gives Sheerpower something it can evaluate to one answer.

Simple Numeric Expressions

A numeric expression produces a number.

print 10 print 10 + 5 print 10 - 5 print 10 * 5 print 10 / 5

Sheerpower evaluates each expression before printing the result.

base_price = 100 tax = 8.75 print base_price + tax

The expression base_price + tax is worked out to one numeric value.

Text Expressions

A text expression produces a string.

print "hello" print "Good "; "morning" print "Employee: "; employee(name)

Text expressions are commonly used to build messages and labels.

print "Name: "; employee(name) print "Department: "; employee(department) print "Status: "; "active"

Each item separated by a semi-colon is an expression. Sheerpower evaluates each expression and prints the results together.

Printing Multiple Expressions

A print statement can print one expression or several expressions. When printing several expressions, Sheerpower best practice is to separate them with semi-colons.

your_name$ = "Dan" print "Your name is "; your_name$ print "Employee: "; employee(name) print "Salary: "; employee(salary) print "Monthly salary: "; employee(annual_salary) / 12

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.

Variables Can Be Expressions

A variable by itself is an expression because Sheerpower can look up its current value.

quantity = 12 price = 4.95 print quantity print price print quantity * price

In the last line, quantity * price is also an expression. Sheerpower gets the value of each variable, multiplies them, and produces one answer.

Table Fields Can Be Expressions

In Sheerpower, a table field can be used directly in an expression using the table-name and field-name syntax:

employee(salary)

This means the salary field in the current employee record.

A table field by itself is an expression:

print employee(name) print employee(salary) print employee(department)

Each one produces a single value from the current employee record.

Table Fields in Calculations

Table fields become especially useful when they are combined with other values.

print employee(salary) + 100 print employee(annual_salary) / 12 print employee(salary) * 12 print employee(hours) * employee(rate)

Each of these is an expression:

  • employee(salary) + 100
  • employee(annual_salary) / 12
  • employee(salary) * 12
  • employee(hours) * employee(rate)

Sheerpower evaluates the table fields, performs the calculation, and produces one answer.

Business Examples

Expressions are used constantly in business programs.

print customer(balance) print customer(balance) + late_fee print invoice(quantity) * invoice(unit_price) print employee(annual_salary) / 12

These expressions represent common business values:

  • a customer's current balance
  • a balance plus a late fee
  • an invoice line total
  • an invoice total with tax
  • an employee's monthly salary

Expressions Can Be Assigned

An expression can be evaluated and assigned to a variable.

monthly_salary = employee(annual_salary) / 12 bonus_amount = employee(salary) * .10 new_salary = employee(salary) + 1000 print "Monthly salary: "; monthly_salary print "Bonus amount: "; bonus_amount print "New salary: "; new_salary

The right side of each assignment is an expression. Sheerpower evaluates it first, then stores the result.

Expressions Can Be Printed

The print statement can print expressions directly.

print employee(name) print employee(salary) print employee(salary) + 100 print "Annual salary: "; employee(annual_salary) print "Monthly salary: "; employee(annual_salary) / 12

The expression does not need to be stored in a variable first. If Sheerpower can evaluate it, it can be printed.

Expressions Can Be Used in Conditions

An expression can also produce a true-or-false answer.

if employee(salary) > 100000 then print employee(name); " is a high-salary employee" end if

The expression:

employee(salary) > 100000

produces one answer: true or false.

That makes it a Boolean expression.

More Boolean Expression Examples

Boolean expressions are often used with if statements.

if employee(active?) then print employee(name); " is active" end if if customer(balance) > 0 then print customer(name); " owes money" end if if invoice(total) = 0 then print "No amount due" end if if employee(department) = "Sales" then print employee(name); " works in Sales" end if

Each condition is an expression that Sheerpower can evaluate to true or false.

Expressions Can Compare Table Fields

Expressions can compare one table field to another.

if invoice(amount_paid) = invoice(total) then print "Invoice paid in full" end if if invoice(amount_paid) < invoice(total) then print "Balance remaining" end if if employee(hours_worked) > employee(standard_hours) then print "Overtime may apply" end if

Each comparison is a single expression.

Expressions Can Combine Text and Table Fields

A print statement can display literal text and table fields together by separating each expression with a semi-colon.

print "Employee: "; employee(name) print "Department: "; employee(department) print "Customer: "; customer(name) print "Invoice number: "; invoice(number)

This is useful for labels, reports, messages, logs, and screen output.

Concatenation Creates a New String

Sometimes you really do want to create a new string value. In that case, string concatenation is appropriate.

full_name$ = employee(first_name) + " " + employee(last_name) message$ = "Your name is " + your_name$ print "Full name: "; full_name$ print message$

The important distinction is this:

  • Use semi-colons when printing several expressions.
  • Use concatenation when creating one new string value.
// Best for printing several values print "Your name is "; your_name$ // Best when creating one new string value message$ = "Your name is " + your_name$ print message$

Expressions Can Be Used in Report Output

Reports often use expressions to calculate and display values.

print "Employee: "; employee(name) print "Salary: "; employee(salary) print "Raise: "; employee(salary) * .05 print "New salary: "; employee(salary) * 1.05 print "Monthly salary: "; employee(annual_salary) / 12

The report does not need a separate field for every value. Some values can be calculated as expressions when needed.

Expressions Can Use Built-In Functions

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.

print abs(-25) print sqr(144) print round(employee(annual_salary) / 12, 2) print max(employee(salary), employee(previous_salary)) print min(invoice(total), customer(credit_limit))

Built-in functions can also be combined with table fields and ordinary calculations.

monthly_salary = round(employee(annual_salary) / 12, 2) raise_amount = round(employee(annual_salary) * .05, 2) print "Monthly salary: "; monthly_salary print "Raise amount: "; raise_amount

String functions can also be used directly in expressions.

print ucase$(employee(name)) print lcase$(employee(email)) print len(employee(name)) print left$(employee(last_name), 1) print pos(employee(email), "@")

These function calls can be printed, assigned, compared, or used inside larger expressions.

initial$ = left$(employee(last_name), 1) email_at = pos(employee(email), "@") print "Last initial: "; initial$ print "Email @ position: "; email_at if pos(employee(email), "@") > 0 then print "Email address appears to contain @" end if

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 Be Nested

Expressions can contain smaller expressions inside larger expressions.

print (employee(salary) + 1000) / 12 print (invoice(quantity) * invoice(unit_price)) + invoice(tax) print "Employee: "; ucase$(employee(name)) print "Rounded monthly salary: "; round(employee(annual_salary) / 12, 2)

Sheerpower works from the inside outward until the entire expression has one final value.

Parentheses Make Expressions Clear

Parentheses can make an expression easier to read.

print employee(salary) + employee(bonus) / 12 print (employee(salary) + employee(bonus)) / 12

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 in Updating Fields

Expressions are also used when updating values.

employee(salary) = employee(salary) + 1000 employee(hours) = employee(hours) + 1 customer(balance) = customer(balance) - customer(payment) invoice(total) = invoice(subtotal) + invoice(tax)

In each assignment, the expression on the right is evaluated first. The result is then stored into the field on the left.

Expression Examples with Money

Business applications often use expressions involving money.

print order(quantity) * order(unit_price) print order(subtotal) + order(shipping) print order(subtotal) + order(tax) + order(shipping) print customer(balance) - customer(last_payment) print employee(hourly_rate) * employee(hours_worked) print employee(annual_salary) / 12

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.

Expression Examples with Percentages

Percentages are also expressions.

print employee(salary) * .05 print invoice(total) * .0825 print product(price) * product(discount_rate) print customer(balance) * customer(interest_rate)

A raise, tax, discount, or interest charge can all be calculated with an expression.

raise_amount = employee(salary) * .05 new_salary = employee(salary) + raise_amount print "Raise amount: "; raise_amount print "New salary: "; new_salary

Expression Examples with Names

Expressions are not limited to numbers. They can also create text values.

full_name$ = employee(first_name) + " " + employee(last_name) print "Full name: "; full_name$ print "Dear "; employee(first_name); "," print employee(last_name); ", "; employee(first_name)

The assignment creates one new string value. The print statements display several expressions by separating them with semi-colons.

Expression Examples with Dates

Date values can also be used in expressions.

print order(date) print employee(hire_date) print customer(last_payment_date)
See Date and Time Functions for details.

Depending on the business rule, date fields can be displayed, compared, or used in calculations.

if (days(employee(hire_date)) - days(date$)) >= 90 then print employee(name); " Probation period is over" end if

The comparison is an expression that produces true or false.

Expression Examples in Loops

Expressions are often used inside loops.

total = 0 for each employee total = total + employee(salary) next employee print "Total salary: "; total

The expression:

total + employee(salary)

is evaluated for each employee record.

Report Line Examples

A report line often contains several expressions.

print employee(name); employee(department); employee(salary) print employee(name); employee(salary); employee(annual_salary) / 12 print customer(name); customer(balance); customer(balance) * .015 print invoice(number); invoice(total); invoice(total) - invoice(paid)

Each item being printed is an expression.

Common Expression Patterns

Here are some common expression patterns.

// add a fixed amount print employee(salary) + 100 // calculate monthly salary print employee(annual_salary) / 12 // subtract one value from another print customer(balance) - customer(payment) // multiply quantity by price print invoice(quantity) * invoice(unit_price) // calculate tax print invoice(subtotal) * invoice(tax_rate) // print a display name print employee(last_name); ", "; employee(first_name) // round a calculated value print round(employee(annual_salary) / 12, 2) // search inside a string field print pos(employee(email), "@")

These are all expressions:

employee(name) employee(salary) employee(salary) + 100 employee(annual_salary) / 12 round(employee(annual_salary) / 12, 2) pos(employee(email), "@")

Each one can be evaluated to a single answer.

Statement vs. Expression

A statement tells Sheerpower to do something.

An expression gives Sheerpower a value to work out.

print "Monthly salary: "; employee(annual_salary) / 12

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.

More Complete Example

This example uses several expressions to print a simple employee salary summary.

print "Employee Salary Summary" print print "Name: "; employee(name) print "Department: "; employee(department) print "Annual salary: "; employee(annual_salary) print "Monthly salary: "; employee(annual_salary) / 12 print "Rounded monthly salary: "; round(employee(annual_salary) / 12, 2) print "Five percent raise: "; employee(annual_salary) * .05 print "Salary after raise: "; employee(annual_salary) * 1.05 print "Last initial: "; left$(employee(last_name), 1) if employee(annual_salary) > 100000 then print "Salary category: senior range" else print "Salary category: standard range" end if if pos(employee(email), "@") > 0 then print "Email status: contains @" else print "Email status: check email address" end if

This example includes:

  • text expressions
  • table-field expressions
  • numeric expressions
  • Boolean expressions
  • built-in function expressions
  • expressions used in print
  • expressions used in if

Why Expressions Matter

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

print employee(salary) + 100 print "Monthly salary: "; employee(annual_salary) / 12 print "Rounded monthly salary: "; round(employee(annual_salary) / 12, 2) new_balance = customer(balance) - customer(payment) if invoice(total) > 0 then print "Amount due: "; invoice(total) end if

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.

(Show/Hide Sheerpower Expression Takeaways)
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.