Visual Programming
Visual Programming
Many people imagine that all computer languages are difficult. This is not true.
Scratch and Blockly are great ways to learn programming. They both use a visual programming approach with jigsaw-shaped pieces that will only join up in correct ways. This eliminates a lot of errors that programmers make, known as syntax errors. A syntax error occurs when rules for a computer language are broken.
Visual languages are becoming very sophisticated and are being used now to perform advanced programming.
ACTIVITY: 13 - Number Guesser
Using your flowchart or structured English algorithm for the number guesser, create the game on Scratch.
Structured Programming
Like all complex things, computer programs can be understood by breaking them down into simpler parts. A computer program is a set of instructions run by a computer just as a storybook is made up of a collection of sentences. Computer instructions are like turn-by-turn directions you might be given: turn right at the intersection, walk two blocks, keep walking until the first set of traffic lights. The computer follows each instruction you give it.
A Dutch computer scientist, Edsger Wybe Dijkstra, popularised structured programming using only three control structures.
ACTIVITY: Google “Blockly Maze” to discover structured programming in a fun way.
Control Structures: Breaking It All Down
No matter how complex an algorithm is, it is built using just three basic building blocks, called control structures. This is similar to the way many complex Lego structures can be built using just a few shapes.
These three control structures are sequence, branching (or selection) and iteration (or repetition).
Sequence
Sequence is the simplest type of control structure and the only one the earliest computing programs used. It is just one instruction followed by the next.
Branching (selection)
Most programs reach a point where they have to decide what to do next. In order to make the decision, the program asks a question or tests a condition. This is called the branching (or selection) control structure.
In a flowchart, we represent selection using a diamond shape from which two flowlines emerge, one for Yes and one for No. In structured English we use the words IF, THEN, ELSE and ENDIF for selection
Iteration (repetition)
The third control structure in our program is iteration (also known as repetition).
Iteration, or looping, is the most important of all the control structures as it allows a computer to repeat actions effortlessly over and over again.
The iteration structure does not have a special flowchart symbol. We use the diamond-shaped decision box to perform a test, a process rectangle for the action and a looping flowline to show that a section of code repeats.
The type of loop we decide to use depends on how we want it to behave. Two important categories of loops are:
pre-test loops
post-test loops
Pre-test loop
A pre-test loop performs a test before a loop. The program may not enter the loop at all depending on the outcome of the test condition.
In the flowchart to the right, we see that the loop tests the condition, and only executes the loop code while the condition is True, then tests the condition again, and keeps repeating this cycle until the WHILE test condition is False.
This structure is often known as a WHILE loop.
Post-test loop
A post-test loop performs a test at the end of a loop. This type of loop must always perform the loop at least once - the very first time - until a test condition causes the loop to terminate.
In the flowchart to the right, we see that the loop performs the process first and only then tests the condition, repeating the loop code again if it evaluates to False, or exiting when it evaluates to True.