Sheerpower Logo

B.1  Variables, Data Types, Database Access, Declaring Variables, and Comments


Variables, Data Types, Database Access, Declaring Variables, and Comments

Welcome to the fundamentals of SheerPower! In this tutorial, you'll learn how to store information in variables, understand the different types of data you can use, and how to document your code with comments. Let's get started!

1. Understanding Variables

Variables are containers for storing data. In SheerPower, a variable name must begin with a letter and can contain letters, numbers, and underscores. By convention, multi-word variable names use underscores, like tax_rate or full_name$.

2. SheerPower's Core Data Types

Every variable holds a specific type of data. SheerPower's main data types are:

STRING

For text.

"Hello!"

REAL

For numbers.

123.45

BOOLEAN

For true/false.

true

3. Two Ways to Create Variables

You can create variables implicitly using suffixes, or explicitly using the declare statement.

  • Implicitly (with suffixes): This is quick and easy. A suffix tells SheerPower the variable's type.
    • $ for a STRING (e.g., name$)
    • ? for a BOOLEAN (e.g., is_done?)
    • % for an INTEGER (a whole number -- rarely needed, use REAL)
    • No suffix for a REAL (the default for numbers)
  • Explicitly (with declare): This is clearer and helps prevent mistakes.
! Implicitly created variables full_name$ = "Sally Sue" is_active? = true total_cost = 19.95 ! Explicitly declared variables declare string shipping_address declare real weight declare boolean item_available shipping_address = "123 Main St" weight = 2.5 item_available = true

4. A Special Note on `REAL` and Perfect Precision

SheerPower's REAL data type is special. It provides Perfect Precision Math, which means calculations are always exact, just like on a calculator. This prevents tiny rounding errors that can happen in other programming languages.

(0.1 + 0.2) - 0.3
SheerPower: 0
Other Languages: 0.00000000...555

5. Special Variable Types

You can also create constants (variables that never change) and even your own custom data types.

! A constant's value cannot be changed later. const pi = 3.14159 ! Create a new data type named 'money' based on REAL. type money real ! Now you can declare variables of type money. declare money monthly_salary monthly_salary = 5000.00

6. Formatting Your Output

You can define a default output format for a variable, which is especially useful for things like currency.

! This variable will always print with a $ and 2 decimal places. declare format "$%.2m" price price = 123.456 ! It will be rounded to $123.46 when printed print "Total price: "; price

7. A Quick Look at Database Variables

SheerPower has a secure, built-in way to access database records. You use the syntax table_name(field_name). This separates code from data, which helps prevent common security issues like SQL injection.

Payroll Table
id name salary
payroll(salary) = 55000.00

This is a deep topic. To learn more, see the Integrated Database Access tutorial.

8. Documenting Your Code with Comments

Comments are notes for humans that the computer ignores. They make your code easier to understand. Use ! or // to write a comment.

! This is a comment. It explains the next line. const tax_rate = 0.08 // 8% sales tax ! Good comments explain the "why," not just the "what." ! Calculate the final price including tax. final_price = subtotal * (1 + tax_rate)

9. Putting It All Together

Here is a small example that uses several of the concepts we just covered.

! Define a constant for the sales tax rate const sales_tax_rate = 0.08 ! Declare variables to hold item information declare string item_name$ declare real item_price declare integer quantity% ! Assign values to our variables item_name$ = "Super Gadget" item_price = 29.95 quantity% = 2 ! Calculate the subtotal subtotal = item_price * quantity% ! Calculate the final total including tax final_total = subtotal * (1 + sales_tax_rate) ! Print a summary using a formatted string print sprintf("Item: %s, Qty: %i, Total: $%.2m", & item_name$, quantity%, final_total)
Summary: You now know how to create variables, use SheerPower's core data types, add comments, and even format your output. These are the essential building blocks for writing clear, effective, and maintainable SheerPower programs.

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.