Using Selection in Programming
In programming we often need to tell the computer whether to run a section of code, or to skip it. For example, when we run a Bubble Sort, we want to increase the swap counter by 1 if two numbers are swapped over, but if they’re not we want to ignore that line of code. This is where selection comes in.
Selective statements come in two forms: IF, THEN, ELSE and CASE.
In fact, both of these statements work in very similar ways, but the IF statement becomes inefficient as it gets longer. But why?
Imagine that you wanted to look at the value of a variable to see what fruit you had and output a specific statement for each. An IF statement would look at each one in turn then run the line of code that matched the true condition. In pseudocode, this looks something like:
IF fruit = Banana THEN OUTPUT “Yellow” ELSE IF fruit = Apple THEN OUTPUT “Green” ELSE IF fruit = Strawberry THEN OUTPUT “Red” ELSE OUTPUT “I don’t know that fruit”
The code in this case checks the condition in the top statement to see if it is true, if it isn’t it will check the next one down, then the next, and so on until it reaches the ELSE which catches everything else.
Try it for yourself with the code below. Extend the Python 3 code to match the algorithm. (this is not part of your assignment)
A CASE statement is similar to this, but instead of testing a condition for each one from the top down, it will go directly to the correct statement and run the code in that block. In pseudocode this looks like this:
CASE fruit of: Banana: OUTPUT “Yellow” Break; Apple: OUTPUT “Green” Break; Strawberry: OUTPUT “Red” Break; DEFAULT: “I don’t know that fruit”
Notice how they so really similar things, but the CASE statement is much shorter and clearer? So why don’t we use them everywhere? Well, unless there are more than two ELSE IF statements, it doesn’t make much difference and certain languages (like Python) don’t actually contain a CASE statement within the syntax. In those cases, we must stick to long ELSE IF statements, but be aware that a CASE would have been a better choice.
Coding Challenge 1 (Assignment):
Python is an example of a coding language that doesn’t use CASE statements. Complete the code below to include a conditional statement that calculates the grade boundaries of a test (these are example boundaries). The practice version of the code is below – please submit your final code answer using the Tutor Marked Assignment Trinket page)
- 85% = 9
- 79% = 8
- 75% = 7
- 68% = 6
- 60% = 5
- 53% = 4
- 45% = 3
- 38% = 2
- 29% = 1
Coding Challenge 2 (extension):
Combining your selection statements with the parameters that are passed into a sub-routine can mean that you can make your sub-routines more efficient.
The code below sends the number 5 into the sub-routine to output the ASCII Art image. Update the program so that it generates a random number between 1 & 5 and outputs a corresponding image.
This is and extension task & not part of your tutor marked assignment.