We use selection to create code which only runs under the correct conditions.
The most common ways to do this are to use if statements, or to use switch statements. This section covers if statements.
This code allows the user to input a number. It then prints a message only if the number is negative:
INT number = INPUT() IF number < 0 THEN PRINT("The number is negative") ENDIF PRINT("Done")
After inputting the number, the program uses an if statement to check if the number is less than 0.
If the condition is true (for example if the user input the number -3, which is less than 0), then the body of the if statement will run, printing the message.
If the condition is false (for example if the user input the number 5, which is not less than 0), then the body of the if statement will not run, so the message will not be printed.
{{% purple-note %}} The body of the if statement is the code between the IF ... THEN statement, and the ENDIF statement. This code only runs if the condition in the if statement is true. {{% /purple-note %}}
The final statement is outside the body of the if statement, so it always suns. It prints "Done".
Here is a short video which goes through the code, line by line:
There are 6 basic conditions:
An if-else statement allows us to run one section of code if the condition is true, or a different of code if the condition is not true. For example:
INT number = INPUT() IF number < 0 THEN PRINT("The number is negative") ELSE PRINT("The number is positive") ENDIF PRINT("Done")
{{% green-note %}} An if-else will always do one thing or the other. It will never do both, and it will never do nothing. {{% /green-note %}}
Here is a flowchart of the if-else code:
The only problem with the previous code is that it doesn't work in the case when number is 0. The if statement checks if number is less than zero (which it is not), and then says that 0 is positive.
Of course, 0 is neither negative nor positive, it is just 0. There are 3 possible cases, and we handle this using ELSEIF:
INT number = INPUT() IF number < 0 THEN PRINT("The number is negative") ELSEIF number == 0 THEN PRINT("The number is zero") ELSE PRINT("The number is positive") ENDIF PRINT("Done")
Here, we have added an extra check. Here is what the code does, in English:
{{% yellow-note %}} If the number is less than 0 print negative, otherwise if it is equal to 0 print zero, otherwise print positive. {{% /yellow-note %}}
You can add as many ELSEIF statements as you need. The code will execute the first statement which tests for true, and if none are true it will execute the ELSE statement.
{{% blue-note %}} When you use ELSEIF, exactly one statement will be executed, never more, never less. {{% /blue-note %}}
Here is a flowchart of the elseif code:
Sometimes, the problem you are trying to solve doesn't fit neatly into the structure of if-elseif. For example, let's write some code to work out if a particular year is a leap year or not.
The rules are:
So:
Here is how we could code this as a nested for loop:
INT year = INPUT() IF year divides by 4 THEN IF year divides by 100 THEN IF year divides by 400 THEN PRINT("Leap year") ELSE PRINT("Not leap year") ENDIF ELSE PRINT("Leap year") ENDIF ELSE PRINT("Not leap year") ENDIF PRINT("Done")
{{% orange-note %}} You can use the modulo operator to check if the year divides by a particular number. {{% /orange-note %}}
An elseif statement can always be converted to a nested if:
INT number = INPUT() IF number < 0 THEN PRINT("The number is negative") ELSE IF number == 0 THEN PRINT("The number is zero") ELSE PRINT("The number is positive") ENDIF ENDIF PRINT("Done")
It is better to use elseif in this case. The benefits are:
You could also code the leap year case as an elseif:
INT year = INPUT() IF year divides by 400 THEN PRINT("Leap year") ELSEIF year divides by 100 THEN PRINT("Not leap year") ELSEIF year divides by 4 THEN PRINT("Leap year") ELSE PRINT("Not leap year") ENDIF PRINT("Done")
While this code is shorter than the original, some people might find it less clear because the logic of the actual algorithm has been rearranged quite a lot. In cases like this, you should do whatever you think is easier to read.
Copyright (c) Axlesoft Ltd 2021