A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Selection





An important part of programming is the use of selection, that is the ability to do something if certain criteria is met. This may be as simple as increasing your health bar in a computer game if you eat a chicken drumstick or inserting the cooling rods into the nuclear reactor if the temperature exceeds a certain value.

IF Statement


The most common selection statement is the IF statement, the idea is that you compare a value to some criteria, IF the value and criteria match then you proceed in a certain way, otherwise you do something else. For example: If It is the queen Then Salute her Else Treat them like a commoner End

The Else part is optional, you can just ignore the commoner! (and dump the Else)

You might also want to test multiple things in the If statement. For example:

Write a single IF statement for the following:

Ask a user for their eye colour, if they say green call them a "Goblin", else they must be a different type of monster:

What eyes have thee? Green Thou art a Goblin?

Alternatively:

What eyes have thee? Blue Pray tell, be thou another form of beast?

Try the code by inputting "green". It doesn't work! We need to adjust the IF statement: If eyes = "Green" or eyes = "green" Then the Or part makes sure it picks up upper and lower case letters alternatively we could use UCase we'll find out more about this later

How old are you: 11.8 You're probably at secondary school

How old are you: 9 You're not at secondary school

How old are you: 19 You're not at secondary school

Using one IF statement write code to handle the above. HINT: you might need more than one clause in the IF ... THEN section.

Now for some very simple AI:

How do you feel today: Happy or Sad? Sad Have you had some exercise: Yes or No? No Go for a walk, you might feel better

In all other situations the program should say: "Sorry I don't know how to help". Using one IF statement write code to handle the above:

Nested Ifs
Sometimes when we are trying to write complex code we will need to use a combination of IFs. In the example above we might want to still treat an under-age queen with respect, an under-age commoner with contempt, serve an 18+ queen with respect, and serve an 18+ commoner with common manners. In fact it seems as if we need 4 different IF statements. We could solve it like this:

This seems awfully cumbersome and we will now look a more elegant way of solving this, using Nested IF's. First of all, nested means placing one thing inside another, so we are going to place an IF inside another. Try the examples above with the following data, both solutions should provide the same answer: name = "Queen" age = 18 name = "Quentin" age = 28 name = "Queen" age = 17 name = "Aashia" age = 15

Write nests of IF statements for the following:

A car can be hired when a person is over 21 and not intoxicated.

How old are you? 22 Good, that's old enough. Have you been drinking? Yes Come back tomorrow

It should also handle:

console.writeline("How old are you?") 20 console.writeline("You're too young I'm afraid. Come back in a few years")

Create a login screen to do the following:

Enter username Jonny5 RECOGNISED! Enter password: Alive Please enter Jonny5

If they get the username wrong it should immediately kick them out and not ask for the password. If they get the password wrong it should kick them out.

As you should be aware by now a lot of programming is doing things as quickly as possible. You might be fed up with writing long if statements, having to keep hitting that enter key to make new lines. There is a faster way to code: single line IF statements.

This is a much shorter way of writing: But be careful, code like this can often be harder to read and therefore debug. Once it has been through the interpreter / compiler it almost certainly won't be running any faster either, it's just there for you to save a little space. For the exam keep to the longer version.

Case Statement


The other type is the Case statement, this can be summarised by several if statements where the value is compared to several criteria and the action of first criteria matched is performed, otherwise a default action may be performed. Case Enter Restaurant and pick up menu If Egg and Chips available Then Order Egg and Chips End If If Pie and Chips available Then Order Pie and Chips End If If Curry and Chips available Then Order Curry and Chips End If If Pizza and Chips available Then Order Pizza and Chips End If Default Leave hungry End

However, most programming languages will give you a shortened way of implementing a case statement without the need to write all of these if statements. For example in VB.NET we use the

Between 4 and up to 8

Create a program where someone types in the name of an animal and it outputs the sound the animal makes. The animals it should handle are: Try and complete this task by only using 5 case statements.
 * Pig - Oink
 * Cow - Moo
 * Bear - Grr
 * Sheep - Baa
 * Tiger - Grr
 * everything else - Meow

You are now going to use a case statement to create an electronic piano.

Create a case statement in the code below, that will play the notes written above. The while true loop means that the code will never end and will continue for ever. For bonus points try and get the code to accept both upper and lower case inputs Remember to make sound you use: