What is sequencing selection and iteration in computer science

Discover the basic ideas behind computer programs.

Show

    Before we can go too deep into physical computing and creating your own projects, it’s a good idea to have a good grasp of the ideas behind computer programming.

    This isn’t a full course on programming, there are lots of other courses you can do online and even on FutureLearn. xxx suggest course here? This gives you a basic understanding of the concepts behind programming. It will help you later on in the course when you’re creating.

    All programming languages have the same concepts. Regardless of how they’re written, where they run and what they do, they all make use of these basic concepts

    Sequence

    Sequence is the order that commands go in. The order can really matter. Think of the instructions needed to make a cup of tea:

    1. Fill the kettle with water
    2. Turn the kettle on
    3. Put tea bag in cup
    4. Pour hot water into cup
    5. Stir
    6. Remove tea bag
    7. Add milk

    If you did number 3 before 2 then that wouldn’t matter. However, if you did 2 after 1 then you won’t have enough water for your cup of tea.

    This is a very simple example of sequence. We’ll highlight code examples in the actual programs later.

    Selection

    Select can be described as making a choice based on a criteria. Think of it as IF this THEN that.

    It can add new options to your scenario: IF you take milk with your tea THEN add milk to your tea

    Or it can create more efficient scenarios: IF the kettle has no water THEN fill the kettle with water

    Rather than always filling the kettle with water, you’re asking a question first then making a decision based on the answer.

    Selection is an important part of coding. It helps us choose different paths and react to situations.

    Iteration

    Iteration is repeating situations. There are many different types of iteration.

    You can repeat a task a set number of times: Stir the tea 3 times

    You can repeat a task until something happens: Stir the tea until the milk has mixed into the tea

    We will highlight these different types of repetition in the upcoming projects.

    Let’s look at our cup of tea task with Sequence, Selection and Iteration applied. We have the same number of steps but our process has become more precise (how long to stir for), efficient (we don’t fill the kettle if it has water in it already) and we have choice (milk or no milk)

    1. IF the kettle has no water THEN fill the kettle with water
    2. Turn the kettle on
    3. Put tea bag in cup
    4. Pour hot water into cup
    5. Remove tea bag
    6. IF you take milk THEN add milk
    7. Stir until the milk has mixed into the tea

    Where else in your daily life can you see sequence, selection and iteration? These are not just programming concepts, these apply everywhere. Discuss in the comments a task you’ve noticed has one or all of these concepts. Any new ones you’ve just discovered?

    When you write lines of code, there are three ways you can control the order these lines will be executed by the computer:

    1. Sequencing: This means that the computer will run your code in order, one line at a time from the top to the bottom of your program. It will start at line 1, then execute line 2 then line 3 and so on till it reaches the last line of your program.
    2. Selection: Sometimes you only want some lines of code to be run only if a condition is met, otherwise you want the computer to ignore these lines and jump over them. This is achieved using IF statements. e.g. If a condition is met then lines 4, 5, 6 are executed otherwise the computer jumps to line 7 without even looking at line 4,5 and 6.
    3. Iteration: Sometimes you want the computer to execute the same lines of code several times. This is done using a loop. There are three types of loops: For loops, while loops and repeat until loops. That’s handy as it enables you not to have to copy the same lines of code many times.

    Take the quiz (open full screen):