Decision-making with If Statements
Last updated
Last updated
The previous section introduced the concept of "branching", which are a way of instructing programs to only perform certain tasks in certain situations. If you skipped it, or need a refresher, .
The program we are going to create in this section is a simple traffic controller. We will output a colour depending on the current traffic condition, and we will progressively improve our program throughout the section.
A traffic controller is a program that displays a colour (either red, amber or green), depending on the traffic condition on a road junction or intersection.
The first traffic controller we are going to create will consist of a variable we will call condition and we will display:
red, if condition is "stop"
amber, if condition is "ready"
green, if condition is "go"
In order for our controller to know what to display, we will use the If statement. The If statement is used to tell our program to do certain only if a certain condition is true. We use if statements in real life:
"if it rains today, I will not go out"
"if I was hungry, I would have eaten"
"if it is a weekday, I will have to go to school"
We want to create a variable we will call traffic_controller. Next, we will use the if statement to check whether traffic_condition is "stop" (using the equality operator (==) we learned under Data Types > Strings .
Let's see what each line of our program does.
creates a new variable called traffic_condition which stores a value of "stop" (which is a string)
empty line (we use empty lines like this to make it easier to read our program)
check whether traffic_condition has a value of "stop" or not
if traffic_condition has a value of "stop", then print "red"
tell the computer that our if statement has ended (like a full-stop or period)
empty line
If we type this in a .sl file and run our program, we'll get "red" back.
The If statement can look strange when you first encounter it, but as you write more and more programs, you will become used to its strangeness and you will begin to understand why it looks the way it does.
The If statement has 3 parts:
The if keyword
The condition (or expression)
The decision (or consequence)
A keyword is a word that has a special meaning in Sonar. You are not allowed to create variables with any keywords as their names. Examples of keywords are: let, if, else, while, for, true and false.
The if keyword should always be followed by a left parenthesis (. This tells the computer that you are about to start writing the condition part of your If statement.
Your condition should always be surrounded by left and right parentheses ().
After writing your condition, your decision should be written between two curly braces {}. This tells the computer that you are writing the decision part of your If statement.
Now that we know how to write If statements, we can complete our traffic controller program:
Changing the value of traffic_controller to either "stop" or "go" will output either "red" or "green". It is important to note that the program will never output more than one colour (because traffic_condition can only have one value).
In programming, a "statement" is the equivalent of a verb in normal languages like English. Statements are used to perform actions. We have already seen the Let statement which creates variables (see ).