Tutorial 6

Selection: IF & CASE

9618 A-Level


Often, we want to choose what code executes based on some conditions at runtime - for this, we can use the following selection statements

  • IF: code inside block will be executed if IF condition evaluates to TRUE. IF statements can be used for any type of condition, but require more code than CASE statements
  • CASE: offers more concise syntax for checking individual, multiple values or finite ranges (INTEGERs or CHARs)

1

IF Statements

Let's see how to write a simple IF statement

DECLARE a, b : INTEGER a <-- 5 b <-- 4 IF a > b THEN OUTPUT "a is bigger than b" ELSE OUTPUT "b is bigger than a" ENDIF

2

Nested IFs

Note: the syllabus doesn't support ELSE IF, hence you have to nest IF statements inside the ELSE blocks if you want to check multiple conditions

This means that every IF statement should have a corresponding ENDIF - i.e. if you have 5 IFs, then you need 5 ENDIFs

DECLARE a, b, c : INTEGER a <-- 6 b <-- 3 c <-- 8 IF a > b AND a > c THEN OUTPUT "a is bigger than b and c" ELSE IF b > a AND b > c THEN OUTPUT "b is bigger than a and c" ELSE IF c > a AND c > b THEN OUTPUT "c is bigger than a and b" ELSE IF a = b AND b = c THEN OUTPUT "All numbers are the same..." ELSE OUTPUT "Two of the numbers are tied for the biggest" ENDIF ENDIF ENDIF ENDIF

3

CASE Statements

As can be seen, nested IFs can get long quite quickly - in the following cases, we can use CASE statements to offer more concise syntax:

  • to check individual values
  • to check multiple values, separated by a comma ,
  • to check a finite range of INTEGER or CHAR values using TO

Note: in the syllabus document, only the first option is officially listed, however many past paper questions and mark schemes use the latter two as well, so it should be fine to use them in the exam

If none of the values equal the value you are checking, then the OTHERWISE keyword can be used to define the code that gets executed

Some students think you can only have a single line/statement for each case condition - but as you can see in the OTHERWISE block, we can have as many lines as we want inside each condition

DECLARE age : INTEGER age <-- 101 CASE OF age 0,1: OUTPUT "You are a baby 👶" 2 TO 12: OUTPUT "You are a child 🧒" 13 TO 19: OUTPUT "You are a teenager 🧑" 20 TO 99: OUTPUT "You are an adult 🧑" 100: OUTPUT "You are a centenarian 💯" OTHERWISE: IF age > 100 THEN OUTPUT "Over 100. Incredible! 🍾" ELSE OUTPUT "Age can't be negative" ENDIF ENDCASE

4

User Login

Create a program that stores the real username and password in two variables - then asks the user to enter their username/password - if correct, a "Login" message should be displayed, else, appropriate "Username incorrect" or "Password incorrect" message should be displayed

DECLARE realUsername, enteredUsername, realPassword, enteredPassword : STRING realUsername <-- "pseudocode" realPassword <-- "pro" OUTPUT "Enter username" INPUT enteredUsername OUTPUT "Enter password" INPUT enteredPassword IF enteredUsername = realUsername THEN IF enteredPassword = realPassword THEN OUTPUT "Logged in!" ELSE OUTPUT "Password incorrect" ENDIF ELSE OUTPUT "Username incorrect" ENDIF

5

Number, Lowercase, Uppercase

Prompt the user to enter a single character, then output if the character they entered is a number, lowercase letter, uppercase letter or other

DECLARE c : CHAR OUTPUT "Enter a character:" INPUT c CASE OF c 'A' TO 'Z': OUTPUT c, " is an uppercase letter" 'a' TO 'z': OUTPUT c, " is a lowercase letter" '0' TO '9': OUTPUT c, " is a number" OTHERWISE: OUTPUT c, " is non-alphanumeric" ENDCASE

6

Number Challenge

Create a program that asks a user to enter 2 numbers, then outputs "Success" if a * b is 5x greater than a + b - if not, then "Failure" will be output

Hint: a * b = 100 works for this - would what be the values of a and b?

DECLARE a, b : INTEGER OUTPUT "Enter first number:" INPUT a OUTPUT "Enter second number:" INPUT b IF a * b = 5 * (a + b) THEN OUTPUT "Success" ELSE OUTPUT "Failure" ENDIF