Selection allows us to be fussy about what code we run, and when. It allows events to occur if certain conditions are met.
Using the ‘making a cup of tea’ analogy, think about adding milk or sugar. We want to have the choice about whether milk and sugar are added to the tea, and not make an assumption. They act like questions e.g. Is sugar required?
In programming, we ‘ask a question’ using an ‘IF’ statement.
IF answer to ‘Would you like sugar’ = True
don’t add sugar.
Selection allows us to add a level of complexity and interactivity that otherwise wouldn’t be possible.
In this example of code, the use of selection (an ‘if’ statement) allows us to turn a Sparkle to green, when the button is pressed. When the button isn’t being pressed the Sparkle will be purple.
The use of selection is evident within most programs, and not much is achievable without it. Head over to our projects pages to put this into practice.