Handling conditions
Contents
Handling conditions#
Your code needs the ability to take different actions based on different conditions#
Operator |
Description |
|---|---|
|
equal to |
|
not equal to |
|
less than |
|
less than or equal to |
|
greater than |
|
greater than or equal to |
Price <- 1.2
IF Price >= 1.00
THEN
Tax <- 0.07
OUTPUT Tax
ENDIF
You can add another action using ELSE when conditions not met#
Price <- 0.9
IF Price >= 1.00
THEN
Tax <- 0.07
OUTPUT Tax
ELSE
Tax <- 0.0
OUTPUT Tax
ENDIF
Be careful when comparing strings#
Caution
String comparisons are case sensitive
Country <- "CANADA"
IF Country = "canada"
THEN
OUTPUT "Oh look a Canadian"
ELSE
OUTPUT "You are not from Canada"
ENDIF
Use string functions to make case insensitive comparisons#
Country <- "CANADA"
IF LCASE(Country) = "canada"
THEN
OUTPUT "Oh look a Canadian"
ELSE
OUTPUT "You are not from Canada"
ENDIF
Conditions allow our code to react to different situations#
Apply appropriate state or federal taxes based on location
Calculate salary based on job level
What to do if a file is not found
What to do if an expected value is missing
Exercises#
Exercise
Write a Boolean expression equivalent to the statement:
10 times 7 is equal to 70
And then display the result.
// Click 'edit' button to input your code.
Exercise
What is printed when the following statements are executed?
A <- 24 / 8 B <- 24 + 8 OUTPUT A == B
Exercise
Write a program tot input the length and width of a maze. If the two sides are equal, print “The maze is square”. If the two sides are not equal, print “The maze is rectangular”.
Include at least two comments in your program.
// Click 'edit' button to input your code.