levo

Problem Solving and Programming

Programming techniques: Programming constructs

  • 2 things to know
  • Component 02

Overview

Recall questions on sequence, branching and iteration, count-controlled and condition-controlled loops, tracing and nested loops, infinite loops, elseif order and switch/case, plus reading and writing text files, string length and substrings, and the MOD, DIV and ^ operators.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

The three basic programming constructs are sequence, iteration and branching. What does each one do to the order in which statements run?

Key term

You are rewriting a count-controlled loop as a condition-controlled one. What three things must you supply, and which one do people forget?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    The three basic programming constructs are sequence, iteration and branching. What does each one do to the order in which statements run?

    Model answer

    Sequence: statements are carried out one after another in the order they are written, each exactly once. Branching (selection): a Boolean condition is tested and the program takes one of two or more alternative paths, so some statements are skipped altogether. Iteration: a block of statements is repeated, either a set number of times or until a condition changes. Every algorithm can be built from these three constructs alone.

  2. 2

    Work through this and say what is printed, then explain why the order of the lines matters. price = 20 total = price * 3 price = 5 print(total)

    Model answer

    It prints 60. total was worked out while price still held 20, and assigning 5 to price afterwards does not re-run that calculation. In a sequence each line uses the values that exist at the moment it runs, so moving price = 5 above the calculation would make the program print 15 instead. This is why sequence is a construct in its own right: the same three statements in a different order give a different result.

  3. 3

    Explain the difference between count-controlled and condition-controlled iteration, and give a situation that suits each.

    Model answer

    Count-controlled iteration repeats a fixed number of times that is known before the loop starts, using a counter that is stepped each time round (for example, for i = 1 to 10). It suits work where you already know how many repetitions are needed: processing every element of an array of 50 records, printing a times table, drawing a shape with a known number of sides. Condition-controlled iteration repeats while, or until, a condition holds, so the number of repetitions is not known in advance. It suits validating input until the user types something acceptable, reading lines until the end of a file is reached, or a game loop that runs until the player quits.

  4. 4

    A loop is written as for i = 0 to 7. How many times does the body run, and why is this a classic place for an off-by-one error?

    Model answer

    Eight times, with i taking the values 0, 1, 2, 3, 4, 5, 6 and 7, because both end values are included. It is a classic error because someone reading it as 'up to 7' may expect seven repetitions. It is also where array indexing catches people out: arrays here are numbered from 0, so an array of eight items has its last item at index 7 and this loop is exactly right for it, whereas for i = 0 to 8 would run nine times and try to read an item that does not exist.

  5. 5

    What is the one behavioural difference between a while loop and a do ... until loop, and when does it matter?

    Model answer

    A while loop tests its condition before the body, so if the condition is false at the very start the body never runs at all. A do ... until loop tests after the body, so the body always runs at least once. It matters whenever running the body once with no valid data would be wrong. 'While there are records left, process one' must be a while loop, because an empty file should cause no processing. 'Ask for a password and repeat until it is correct' suits do ... until, because you have to ask at least once before there is anything to test.

  6. 6

    Trace this and give the two numbers printed. count = 0 total = 0 while total < 20 count = count + 1 total = total + count endwhile print(count) print(total)

    Model answer

    It prints 6 then 21. Working through: count 1 gives total 1; count 2 gives total 3; count 3 gives total 6; count 4 gives total 10; count 5 gives total 15 — still under 20, so the loop runs once more; count 6 gives total 21, the condition total < 20 is now false and the loop ends. Note that the loop finishes with total past 20 rather than exactly on it, because the test only happens between whole repetitions.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Problem Solving and Programming, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.