Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies · Design and Make: Grade 7 Researchers, Drafters and Makers

Computational Thinking: Programming Languages, Variables and Loops

BC ADST 7: programming languages BC ADST 7: visual programming BC ADST 7: simple algorithmsAbout 45 minutes
Learning goals
  • I can describe how programming languages have changed, from machine code to visual programming.
  • I can use variables, loops and conditions in a visual program.
  • I can trace an algorithm with a trace table and represent it visually.

Words to know

programming language
A set of words and rules for writing instructions a computer can follow.
machine code
Instructions written as binary numbers (0s and 1s) that a processor runs directly.
high-level language
A language that uses words and symbols closer to human language, which is translated into machine code.
variable
A named place to store a value that can change, such as score or total.
loop
Code that repeats, either a set number of times or until something happens.
trace table
A table that tracks the values of variables step by step as an algorithm runs.

From switches to blocks

A processor only understands machine code: long strings of 0s and 1s. The first programmers in the 1940s wrote instructions this way, or by setting switches and plugging cables. It was slow, and one wrong digit could wreck a program.

Over time, people built languages that are easier for humans:

  • Assembly language replaced number codes with short words such as ADD and MOV.
  • High-level languages such as FORTRAN (1957), COBOL (1959) and BASIC (1964) let programmers write instructions closer to English and maths. A program called a compiler or interpreter translates them into machine code. Grace Hopper was a pioneer of compilers, and her work helped shape COBOL.
  • Later languages such as Python (1991) were designed to be easy to read.
  • Visual (block-based) programming languages such as Scratch (2007) let you snap blocks together, so you can focus on the logic instead of typing exact spelling and punctuation.
1940smachine codeearly 1950sassembly1957FORTRAN1959COBOL1964BASIC1991Python2007Scratch
Some milestones in the evolution of programming languages (not drawn to scale).

Variables, loops and conditions

Three tools appear in almost every program:

  • A variable stores a value with a name, such as score = 0. The value can change while the program runs.
  • A loop repeats code: repeat 10 times or repeat until score = 100. Loops are repetition.
  • A condition makes a choice: if score > 50 then say "Great!". Conditions are selection.

Together with sequence (steps in order), these let you build simple algorithms for many tasks, including searching and sorting.

when green flag clicked
set total to 0
set n to 1
repeat 4 times
change total by n
change n by 1
end repeat
if total > 5 then
say (join "Total is big: " total)
else
say (join "Total is small: " total)
end if

A visual program in block style. It adds 1 + 2 + 3 + 4 using a loop and two variables, then uses a condition.

Worked example

Tracing the program

Trace the program above. What does the sprite say?

  1. Start: total = 0, n = 1.
  2. Loop 1: total = 0 + 1 = 1, then n = 2.
  3. Loop 2: total = 1 + 2 = 3, then n = 3.
  4. Loop 3: total = 3 + 3 = 6, then n = 4.
  5. Loop 4: total = 6 + 4 = 10, then n = 5. The loop has run 4 times, so it stops.
  6. Condition: is 10 > 5? Yes, so the program uses the if branch.

The sprite says "Total is big: 10".

Stepn (at start of loop)total (after the loop)
Start10
Loop 111
Loop 223
Loop 336
Loop 4410
A trace table for the program. Tracking variables like this helps you find bugs.

Showing algorithms and data visually

Visual representations help people understand problems and data:

  • A flowchart shows the steps of an algorithm, with diamonds for decisions and arrows for loops.
  • A trace table shows how variables change.
  • Charts and graphs show data from a program, such as a bar graph of scores. Many visual programming tools can draw them.

When a program doesn't work, debug it: trace it by hand, check variable values at each step and fix one thing at a time.

Start the variable before the loop

A very common bug is setting a variable inside the loop. If set total to 0 were inside the loop, total would reset every time and end at 4, not 10. Set starting values before the loop begins.

Hands-on activity

Build a counting program

  1. In a block-based programming tool your teacher or family approves, make a program that counts by 2s from 2 to 20.
  2. Use a variable (count) and a loop. Have the sprite say each number.
  3. Add a condition: when count equals 10, the sprite also says "Halfway!"
  4. Before you run it, make a trace table for the first 4 loops.
  5. Run the program. Did it match your trace table? If not, debug.
  6. No device? Write the blocks on paper and have a partner act as the computer.
Joke

There are 10 kinds of people in the world: those who understand binary, and those who don't.

In binary, 10 means two: one 2 and zero 1s.

Let's practise

1

Why is machine code hard for people to write?

2

What does a compiler or interpreter do?

3

Name one advantage of block-based programming.

4

What is the value of x after this: set x to 3, change x by 4, change x by -2?

5

Trace: set total to 0; repeat 3 times: change total by 5. What is total?

6

Is if lives = 0 then stop game an example of sequence, selection or repetition?

More practice

1

Put in order, oldest first: Python, machine code, FORTRAN, Scratch.

2

Trace: set n to 1; repeat 3 times: set n to n x 2. What is n?

3

In a flowchart, what shape usually shows a decision?

4

A program should add 2 + 4 + 6 but always says 6. The set total to 0 block is inside the loop. Explain the bug.

5

What is 1101 in binary as a regular number? (Place values 8, 4, 2, 1.)

Check your understanding

Quiz

1.

Which came first?

2.

Trace: set total to 0; repeat 4 times: change total by 3. What is total?

3.

A named place that stores a value that can change is a...

4.

repeat until score = 100 is an example of...

For parents and teachers

How to help at home: Ask your child to explain one of their programs to you, block by block, and trace it together with a trace table on paper. Play "human computer": they give you exact instructions and you follow them literally.

Common mistakes: Setting starting values inside a loop, miscounting how many times a loop runs, mixing up selection and repetition, and changing several blocks at once while debugging.