Sheerpower Logo
O.2  Data Types, Declaring Variables, and Comments

Lesson 2: Variables, Data Types, Database Access, Declaring Variables, and Comments

First and foremost, have fun when programming with Sheerpower. It is fast. It is intuitive.

1. Variable Names

In Sheerpower, variable names must start with a letter and can include letters, numbers, the underscore ("_"), and an embedded dollar sign ("$"). Names are case-insensitive, meaning Account is the same as AccOUNT. By convention, if a variable name consists of multiple words, each word is separated by an underscore.

Variables with a dollar sign "$" suffix default to being a string. Those with a percent sign "%" suffix default to being an integer. And those with a question mark "?" suffix default to being a boolean. Unsuffixed, undeclared variables are assumed to be declared as real.
tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code demonstrates variable naming conventions in Sheerpower.

2. Primitive Data Types

Sheerpower supports several primitive data types:

  • Real: A number with up to 18 digits to the left of the decimal and 16 to the right. Sheerpower's Perfect Precision Math ensures exact arithmetic operations, eliminating rounding errors.
  • String: Used to store characters, such as a person's name or city name.
  • Integer: A whole number without fractional digits.
  • Boolean: A logical value of true (1) or false (0).
  • Dynamic: Can be any data type and adjusts its data type according to use.
tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock."

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code demonstrates how to declare variables of different data types in Sheerpower and how they can be used in your programs.

3. Database Access

In Sheerpower, database access is expressed using the table_name(field_name) syntax. This makes Sheerpower immune to SQL injection attacks. Let's see how database access works in Sheerpower.

tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock." print payroll(salary) payroll(salary) = 1234.56 print payroll(salary)

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. The first line fetches the current payroll table record, locks it if needed, extracts the salary field, and prints its contents. The second line updates the salary field to 1234.56. The third line prints the updated salary.

4. Declaring Variables

You can declare variables in Sheerpower using the declare keyword. This helps to define the data type of the variable and ensures that it is used correctly throughout your program.

tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock." print payroll(salary) payroll(salary) = 1234.56 print payroll(salary) declare dynamic answer declare real inches declare string address$ declare integer frogs% declare boolean is_great? inches = 55.6 print inches

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code demonstrates how to declare variables of different types and use them in your programs.

5. Defining Constants

You can define constants in your code using the const keyword. Constants cannot be accidentally changed once defined.

tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock." print payroll(salary) payroll(salary) = 1234.56 print payroll(salary) declare dynamic answer declare real inches declare string address$ declare integer frogs% declare boolean is_great? inches = 55.6 print inches const speed_of_light_mps = 299_792_458 print "The speed of light is "; speed_of_light_mps; " meters per second."

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code defines a constant for the speed of light and prints it.

6. Declaring Default Output Formatting

You can declare a default output format for variables using the declare format statement. This ensures that when the variable is printed, it always uses the specified format.

tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock." print payroll(salary) payroll(salary) = 1234.56 print payroll(salary) declare dynamic answer declare real inches declare string address$ declare integer frogs% declare boolean is_great? inches = 55.6 print inches const speed_of_light_mps = 299_792_458 print "The speed of light is "; speed_of_light_mps; " meters per second." declare format '$%.2m' cash cash = 123456.7 print 'The value is: '; cash

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code declares a variable with a custom format and prints it with the specified formatting.

7. Custom Data Types

Custom data types can be defined in Sheerpower using the type keyword. After being defined, custom data types can be used in place of any primitive data type, improving code readability and maintainability.

tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock." print payroll(salary) payroll(salary) = 1234.56 print payroll(salary) declare dynamic answer declare real inches declare string address$ declare integer frogs% declare boolean is_great? inches = 55.6 print inches const speed_of_light_mps = 299_792_458 print "The speed of light is "; speed_of_light_mps; " meters per second." declare format '$%.2m' cash cash = 123456.7 print 'The value is: '; cash type money format '$%.2m' declare refund money refund = 123456.728 print 'Your refund is: '; refund

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code defines a custom data type for money and uses it to format and print a refund amount.

8. Comments in Your Code

Comments in your code help explain the logic and make it easier for others (or yourself in the future) to understand what your code does. Comments are ignored by Sheerpower, so they don't affect how the program runs.

tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare real price declare string product_name$ declare integer quantity% declare boolean in_stock? declare dynamic misc_data price = 19.99 product_name$ = "Widget" quantity% = 100 in_stock? = true misc_data = "Sample text" print product_name$; " costs $"; price; " and we have "; quantity%; " in stock." print payroll(salary) payroll(salary) = 1234.56 print payroll(salary) declare dynamic answer declare real inches declare string address$ declare integer frogs% declare boolean is_great? inches = 55.6 print inches const speed_of_light_mps = 299_792_458 print "The speed of light is "; speed_of_light_mps; " meters per second." declare format '$%.2m' cash cash = 123456.7 print 'The value is: '; cash type money format '$%.2m' declare refund money refund = 123456.728 print 'Your refund is: '; refund // This is a single-line comment ! This is another single-line comment using an exclamation mark const pi = 3.14159 // Define the constant pi radius = 10 area = pi * radius * radius print "The area of the circle is "; area

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code includes comments to explain what each part of the program does.

Summary: In this lesson, you've learned about variable names, primitive data types, database access, declaring variables, defining constants, and using comments in Sheerpower. These are fundamental concepts that will help you write clear, efficient, and maintainable code. Keep practicing to master these concepts!
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.
Wide screen