Sequencing, Selection and Loops
Sequencing in programmingA computer program is simply a set of instructions that a computer follows.
Another word for this is an ALGORITHM. If the instructions are in the wrong order, then the outcome won't be what you expect or want. A computer cannot think so it will only follow the instructions in the order that you give them. This is known as SEQUENCING. The first levels of the maze will seem very easy to you but getting instructions in the wrong order is a very common mistake in programming. During these levels you will also be learning something else that is very important in programming: TESTING. You should test often. It is much easier to fix short pieces of code and to fix one mistake at a time. So keep TESTING until your ALGOITHM is in the correct SEQUENCE and you have achieved the goal. |
|
Repeating instructions - using loops
As soon as you find yourself repeating the same line of code, your programmer's brain should think "there must be a better way of doing this!"
Of course there is a better way and this is known as REPETITION or LOOPING. In programming we use 2 types of loop: one is called a WHILE loop which you can think of as a 'repeat until' loop. The code will run until a condition is met. The other type of loop is called a FOR loop and this repeats code for a set number of times. The code that is to be repeated is always inside a block when using Blockly. But this is also true when we write text-based code. In Python, code is INDENTED inside a loop. This means the computer knows which lines of code need to be repeated. Here are some examples: |
|
Selection - decisions - making choices - if statements
Very often in programming, we want our programs to branch - go one way if something and another way if something else.
Thinking about logging in to your computer. You enter your user name and password. If these 2 match those stored by the computer, you will log in and see your emails or your files. If they don't match, you will see an error message. This is an example of SELECTION. Part of a program which branches down different routes depending on the input. Keywords in selection are IF and ELSE. The code that goes with each is put INSIDE the code block, just like it is with a loop. This means the computer knows when to run a piece of code and when to skip it. |
|