These worksheets are free forever. Want lessons that adapt to your child as they learn, plus progress tracking? Try Ignition Learning free.

Sign up free

Ignition Learning — Activity Sheet

Programming: variables, loops & functions

Technologies · Year 8

Name: ______________________Date: ____________

Programs are built from a small set of powerful building blocks. A variable stores a piece of data (like a number or word) that can change while a program runs — think of it as a labelled box holding a value. A loop repeats a set of instructions multiple times, avoiding the need to write the same code over and over (a "for" loop repeats a fixed number of times; a "while" loop repeats until a condition becomes false). A function is a named, reusable block of code that performs a specific task — you can call it whenever needed instead of rewriting the same instructions each time, making programs shorter, clearer and easier to fix.

Example

Instead of writing 'print hello' 10 separate times, a for loop repeats 'print hello' 10 times with one short block of code. A function called calculateArea(length, width) can be written once and then called repeatedly throughout a program whenever an area needs calculating, using a variable to store each new result.

Key terms

Variable:
A named storage location for a piece of data that can change.
Loop:
A structure that repeats a set of instructions multiple times.
Function:
A named, reusable block of code that performs a specific task.

Questions

  1. 1. A variable is:

    • A named storage location for a piece of data that can change
    • A fixed value that never changes
    • A type of hardware
    • Something unrelated to programming
  2. 2. A loop is used to:

    • Repeat a set of instructions multiple times
    • Run code exactly once only
    • Delete all code
    • Store a single fixed value
  3. 3. A function is:

    • A named, reusable block of code that performs a specific task
    • A type of variable only
    • Something that can never be reused
    • A type of hardware component
  4. 4. A "for" loop typically repeats:

    • A fixed number of times
    • An undefined number of times with no condition
    • Only once
    • Never
  5. 5. A "while" loop repeats until:

    • A condition becomes false
    • The program crashes
    • A random number is reached
    • Nothing, it never repeats
  6. 6. Using a function instead of repeating code makes a program:

    • Shorter, clearer and easier to fix
    • Longer and harder to understand
    • Impossible to run
    • Slower with no other effect
  7. 7. A variable can be thought of as:

    • A labelled box holding a value
    • A fixed, unchangeable rule
    • A type of loop
    • Something with no connection to data
  8. 8. Why might using a loop to print "hello" 10 times be more efficient than writing the print instruction 10 separate times?

    • It avoids repetitive code, making the program shorter and easier to modify (like changing the count) in one place
    • Loops always make a program longer and harder to modify
    • Writing repeated code separately is always more efficient than using a loop
    • Loops have no effect on how much code needs to be written
  9. 9. Why might a programmer use a variable to store a calculated area result, rather than immediately printing the raw calculation?

    • Storing the result allows it to be reused, checked, or modified later in the program without recalculating it
    • Variables can never store the result of a calculation
    • Immediately printing a value is always more useful than storing it
    • Storing a value in a variable has no practical benefit
  10. 10. Why might a "while" loop be more appropriate than a "for" loop for a program that keeps asking a user for input until they enter a valid number?

    • The number of repetitions needed isn't known in advance — it depends on when the condition (valid input) becomes true
    • A "for" loop is always better suited to situations with an unknown number of repetitions
    • The choice between loop types never makes any practical difference
    • "While" loops can only ever run a fixed number of times
  11. 11. Why might writing a reusable function like calculateArea() be better than copying and pasting the same calculation code in multiple places?

    • If you need to fix or improve the calculation, you only need to update it in one place, rather than every copy
    • Copying and pasting code is always easier to maintain than a single reusable function
    • Reusable functions have no advantage over duplicated code
    • Functions can never be called more than once in a program
  12. 12. Why might a variable's value change multiple times while a program is running?

    • A variable is designed to store data that can be updated as the program processes new information or user input
    • Variables are always fixed and can never be changed once set
    • Variables can only ever be set once, at the very start of a program
    • Changing a variable's value would cause the program to stop working
  13. 13. Why might breaking a large program into multiple smaller functions make debugging (finding and fixing errors) easier?

    • Each function handles one specific task, making it easier to isolate exactly where a problem is occurring
    • Breaking a program into functions always makes errors more difficult to find
    • A single large block of code is always easier to debug than several smaller functions
    • Function structure has no effect on how easy a program is to debug
  14. 14. A loop that never meets its stopping condition would most likely cause a program to:

    • Run forever (an infinite loop), potentially freezing or crashing
    • Automatically fix itself with no issue
    • Run exactly once as intended
    • Delete itself immediately
  15. 15. Why might a programmer choose to pass different values into the same function (like calculateArea(4, 5) and calculateArea(10, 2)) rather than writing a separate function for each calculation?

    • A single well-designed function can handle many different specific cases just by changing its input values
    • Every slightly different calculation always requires an entirely separate, unique function
    • Passing different values into a function is not actually possible in programming
    • Reusability through changing inputs provides no practical benefit
  16. 16. Why might nesting a loop inside another loop (a "loop within a loop") be useful for processing something like a grid of data (rows and columns)?

    • The outer loop can step through each row while the inner loop steps through each column within that row, systematically covering every cell
    • Nested loops can never be used to process any kind of grid or table
    • A single loop is always sufficient for processing any two-dimensional data
    • Nesting loops has no practical application in real programs
  17. 17. Why might a well-named variable (like "studentScore") be considered better programming practice than a vaguely named one (like "x")?

    • Descriptive names make code easier for the programmer (and others) to read, understand and maintain later
    • Variable names have no effect on how understandable or maintainable code is
    • Vague variable names are always preferred by professional programmers
    • Naming has no real importance in programming practice
  18. 18. Why might understanding variables, loops and functions together be considered foundational to programming, rather than being separate, unrelated skills?

    • These building blocks are combined constantly in real programs to store data, repeat tasks efficiently, and organise code into reusable, logical pieces
    • These three concepts are always used in complete isolation from each other
    • Only one of these three concepts is ever genuinely necessary for programming
    • Combining these concepts together provides no practical advantage in real programs
  19. 19. A programmer writes a function called isEven(number) that returns true or false. Why is designing it as a reusable function better than writing the even/odd check separately every time it's needed in a larger program?

    • It can be called from anywhere in the program whenever that specific check is needed, keeping the logic consistent and in one place
    • A separately rewritten check every time is always more efficient and reliable than a reusable function
    • Functions can only ever be used once within an entire program
    • Consistency across a program has no real benefit for reusable logic
  20. 20. A student writes a loop meant to run 10 times, but a coding mistake makes it run 11 times instead, causing an error at the very last repetition. Why does this kind of "off-by-one" mistake commonly happen in loops?

    • A small miscounting of the starting or stopping condition can easily cause a loop to run one time too many or too few
    • Loops always run exactly the intended number of times with no possibility of miscounting
    • Off-by-one errors only ever occur in "while" loops, never in "for" loops
    • This type of mistake has no real connection to how loop conditions are written
  21. 21. Understanding variables, loops and functions mainly helps you to:

    • Write more efficient, organised and reusable code by using these core programming building blocks
    • Assume every program should be written as one single, unbroken block of repeated code
    • Avoid ever reusing code across a program
    • Treat variables, loops and functions as unrelated to real program structure

Answer key (parent copy)

  1. 1. A named storage location for a piece of data that can change
  2. 2. Repeat a set of instructions multiple times
  3. 3. A named, reusable block of code that performs a specific task
  4. 4. A fixed number of times
  5. 5. A condition becomes false
  6. 6. Shorter, clearer and easier to fix
  7. 7. A labelled box holding a value
  8. 8. It avoids repetitive code, making the program shorter and easier to modify (like changing the count) in one place
  9. 9. Storing the result allows it to be reused, checked, or modified later in the program without recalculating it
  10. 10. The number of repetitions needed isn't known in advance — it depends on when the condition (valid input) becomes true
  11. 11. If you need to fix or improve the calculation, you only need to update it in one place, rather than every copy
  12. 12. A variable is designed to store data that can be updated as the program processes new information or user input
  13. 13. Each function handles one specific task, making it easier to isolate exactly where a problem is occurring
  14. 14. Run forever (an infinite loop), potentially freezing or crashing
  15. 15. A single well-designed function can handle many different specific cases just by changing its input values
  16. 16. The outer loop can step through each row while the inner loop steps through each column within that row, systematically covering every cell
  17. 17. Descriptive names make code easier for the programmer (and others) to read, understand and maintain later
  18. 18. These building blocks are combined constantly in real programs to store data, repeat tasks efficiently, and organise code into reusable, logical pieces
  19. 19. It can be called from anywhere in the program whenever that specific check is needed, keeping the logic consistent and in one place
  20. 20. A small miscounting of the starting or stopping condition can easily cause a loop to run one time too many or too few
  21. 21. Write more efficient, organised and reusable code by using these core programming building blocks