[<< wikibooks] A-level Computing/AQA/Paper 1/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:


== Relational operators ==
We often want to write IF statements that do some kind of comparison or test. We just did exactly that in the example above with age >= 18 which tests if the value of the age variable is greater or equal to 18.
Most of these operators you will recognise from learning maths but some are slightly different in computing. The following operators are straightforward:

The most important operator that is different in computing is one that you have already used many, many times probably without even noticing, which is the = operator. In most programming languages the = operator is used for assignment, for example if we want to assign the value "bunny" to a variable called animal we write animal = "bunny" which is the same in both VB.NET and Python. These two languages are different when it comes to equals which we saw in the example above testing if the value of the name variable was equal to "Queen". In VB.NET the equals operator is just = whereas Python uses == instead. This can lead to a very common programming mistake when writing Python - if we try to write an IF statement which uses = by mistake:

we will get an error message similar to this:

Finally we need an operator for not equals. In VB.NET we use <> whereas in Python we use !=. Here's an example in the form of a really quite rubbish game:


== 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 select case