B2.3.1Correct sequence of code instructions
0/8
B2.3 Programming constructsB2.3.1

Correct sequence of code instructions

Construct programs that implement the correct sequence of code instructions to meet program objectives

SL + HL≈ 45 minCommand term: ConstructFree lesson

In this lesson

  • Explaining why the order of instructions determines what a program does
  • Putting instructions in the correct order: initialise, input, process, output
  • Recognising errors caused by a wrong sequence: infinite loops, deadlocks and unchecked input
  • Constructing and correcting short programs

LearnOne instruction after another

    0 of 3

    CodeUpdate, then add

    count = 0
    total = 0
    while count < 3:
    count = count + 1
    total = total + count
    print(total)
    Output
    6

    CodeAdd, then update

    count = 0
    total = 0
    while count < 3:
    total = total + count
    count = count + 1
    print(total)
    Output
    3

    The two programs contain the same statements; only the order of the two lines inside the loop is different. The first adds 1 + 2 + 3, the second 0 + 1 + 2.

    CheckPut the program in order

    Arrange the lines so that the program outputs the price after a 25 % discount (30.0).

    1. Tap the steps below in the correct order.

    CheckSwap two values

    Arrange the lines so that the values of a and b are swapped and the output is 9 5.

    1. Tap the steps below in the correct order.

    LearnWhen the sequence is wrong

      0 of 3

      LabA deadlock in the kitchen

      Ali and Bea both need the cooker and the worktop. Choose who takes the next step. Let each of them grab one resource first and see what happens, then try the other version and compare what happens.

      AliBeaCookerWorktop

      resource held by process process waiting for resource

      Ali
      1. Request Cooker
      2. Request Worktop
      3. Release Worktop
      4. Release Cooker
      Bea
      1. Request Worktop
      2. Request Cooker
      3. Release Cooker
      4. Release Worktop

        Cause a deadlock, recover from it and let every process finish to complete this activity.

        CodeA loop that ends

        lives = 3
        while lives > 0:
        print("Lives left:", lives)
        lives = lives - 1
        print("Game over")
        Output
        Lives left: 3
        Lives left: 2
        Lives left: 1
        Game over

        If the line that subtracts 1 from lives were placed after the loop instead of inside it, lives would stay 3 and the loop would never end.

        CheckQuick check

        A loop should print the numbers 1 to 5. The counter starts at 1, the loop repeats while the counter is at most 5, and its body prints the counter. Where must the statement that adds 1 to the counter go?

        CodeCheck first, then process

        def grade(mark):
        if mark < 0 or mark > 100:
        return "invalid mark"
        if mark >= 50:
        return "pass"
        return "fail"
        print(grade(72))
        print(grade(30))
        print(grade(720))
        Output
        pass
        fail
        invalid mark

        The range check comes first, so an impossible mark is reported instead of being treated as a pass.

        ExamWorked exam answer

        A program should read a number of tickets, multiply it by the price of 8.50 and output the cost. The programmer’s lines are: output cost; read tickets; cost ← tickets × 8.50. Explain why the program does not work and construct it in the correct order.

        [3]
          Try writing your own answer first.

          ReviewMistakes examiners see

          Misconception

          Initialising a total or counter inside a loop, which resets it on every iteration.

          Misconception

          Printing a result inside a loop when only the final value is wanted.

          Misconception

          Overwriting a variable before its old value has been used or saved.

          Exam tip

          Trace the first two iterations of any loop by hand: most sequence errors show up immediately.

          Exam practiceExam-style questions

          4 questions · 10 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.

          Scenario

          A programmer writes the code below to find the highest score in a list. It should output 12.

          (a)State · AO1[1]

          State the output of the code.

          scores = [12, 7, 9]
          i = 0
          best = 0
          while i < len(scores):
          i = i + 1
          if i < len(scores) and scores[i] > best:
          best = scores[i]
          print(best)
          (b)Explain · AO3[2]

          Explain why the code gives the wrong answer.

          (c)Construct · AO3[4]

          Construct a corrected version as a function (method) that returns the highest score, also working when every score is negative.

          (d)Explain · AO3[3]

          The scores are saved by two programs running at the same time. Program 1 locks the scores file and then the backup file; program 2 locks the backup file and then the scores file. Explain how this sequence can cause a deadlock and how it can be prevented.

          0 / 10

          0 of 4 questions marked. Mark every question to see your total.

          Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.