Sheerpower Logo
B.3  Conditionals

sheerpower Conditionals

IF/THEN ... END IF

The IF/THEN/ELSE/END IF construct in sheerpower is a powerful tool for handling conditional logic. It's used to evaluate one or more conditions and execute specific blocks of code based on the result. For instance, you might want to print different messages depending on a user's age group or gender. In such cases, the IF construct allows for precise control over the flow of your program.

sheerpower is optimized to process conditionals extremely quickly, making it ideal for performance-critical applications. Whether you're checking numeric values or performing string comparisons, sheerpower ensures that evaluations are done efficiently.

One of the unique advantages of sheerpower is its ability to remember string comparison results. If a string comparison is made within a conditional, sheerpower stores the result. If the same comparison is needed again later in the program, it uses the stored result instead of performing the comparison again. This optimization further enhances the speed and performance of your sheerpower code.

SELECT/CASE ... END SELECT

The SELECT construct is particularly useful when you need to compare one main expression against several potential values, executing a different block of code for each possible match. For example, in a tax program, you might need to determine the appropriate tax calculation based on the user's tax bracket. SELECT CASE allows you to compare a main expression, such as tax_bracket, against all the defined tax brackets (e.g., 10000-15000, 15000-25000) and execute the corresponding code for each range.

Like the IF construct, SELECT CASE benefits from sheerpower's performance optimizations. The language is designed to handle multiple comparisons and conditional branches with remarkable speed, making it well-suited for complex decision-making processes in your applications.

if condition then ' code to execute if condition is true end if
if condition then ' code to execute if condition is true else ' code to execute if condition is false end if
if condition1 then ' code to execute if condition1 is true else if condition2 then ' code to execute if condition2 is true else ' code to execute if neither condition1 nor condition2 is true end if

examples:

if x > 10 then print "x is greater than 10" end if
if x > 10 then print "x is greater than 10" else print "x is 10 or less" end if
if x > 10 then print "x is greater than 10" else if x = 10 then print "x is exactly 10" else print "x is less than 10" end if

elseif option:

if condition1 then ' code to execute if condition1 is true elseif condition2 then ' code to execute if condition2 is true elseif condition3 then ' code to execute if condition3 is true else ' code to execute if none of the conditions are true end if
if temperature > 100 then print "temperature is above boiling point" elseif temperature > 0 then print "temperature is above freezing point but below boiling point" elseif temperature = 0 then print "temperature is at freezing point" else print "temperature is below freezing point" end if

select case/end select

select case expression case value1 ' code block for value1 case value2 ' code block for value2 case value3, value4 ' code block for value3 or value4 case is relational_expression ' code block for relational expression case else ' code block if no other cases match end select

examples:

select case grade$ case "a" print "excellent" case "b" print "good" case "c" print "average" case "d", "f" print "poor" case else print "invalid grade" end select
select case score case is >= 90 print "excellent" case is >= 80 print "good" case is >= 70 print "average" case is >= 60 print "pass" case else print "fail" end select

Multiple expressions:

select case fruit$ case "apple", "banana" print "fruit is apple or banana" case "cherry", "date" print "fruit is cherry or date" case else print "other fruit" end select

Ranges with case is:

select case age case is < 13 print "child" case is >= 13 and is <= 19 print "teenager" case is >= 20 and is <= 64 print "adult" case is >= 65 print "senior" case else print "invalid age" end select

Nested select case:

select case department$ case "sales" select case region case "north" print "north sales" case "south" print "south sales" case else print "other sales region" end select case "hr" print "human resources" case else print "other department" end select

IF Construct in sheerpower

The IF construct is useful to check one or more conditions and execute a different block of code depending on the result. For instance, say that you need to print one statement if the user is male and under 20, another if the user is male and between 20 and 40, and still another if the user is male and over 40. The IF construct works well in this kind of situation.

However, in sheerpower, it's important to remember that conditionals must be based on true (1) or false (0 or not true). For example, the following would not be valid:

x = 5 if x then ... end if

The reason is that x is not explicitly 0 or 1. Instead, you should write the condition to ensure it evaluates to true or false:

x = 5 if x <> 0 then ... end if

In this corrected example, x <> 0 evaluates to true (1) if x is not zero, allowing the IF construct to function as expected in sheerpower.

The SELECT construct is particularly useful when you need to compare one main expression against several potential values, executing a different block of code for each possible match. For example, in a tax program, you might need to determine the appropriate tax calculation based on the user's tax bracket. SELECT CASE allows you to compare a main expression, such as tax_bracket, against all the defined tax brackets (e.g., 10000-15000, 15000-25000) and execute the corresponding code for each range.


Here are details on conditionals.

SWITCH and END SWITCH can be used instead of SELECT CASE and END SELECT.
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