HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Variables, Selection and Loops in Pseudocode
  1. 1Variables, constants, data types, input and output
  2. 2Selection with IF and CASE
  3. 3Loops, totals, counts and nested statements
  1. Computer Science
  2. /Variables, Selection and Loops in Pseudocode
  3. /Loops, totals, counts and nested statements

Course 8.1·Lesson 3 of 3

Loops, totals, counts and nested statements

A program repeats steps with a loop. This lesson writes the three loops of the syllabus in Cambridge pseudocode: the count-controlled FOR loop, the pre-condition WHILE loop and the post-condition REPEAT loop. It then uses them for totals, counts and nested statements, with a trace table.

about 30 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
Variables, Selection and Loops in Pseudocode
  1. 1Variables, constants, data types, input and output
  2. 2Selection with IF and CASE
  3. 3Loops, totals, counts and nested statements

Try the code and the simulations beside the lesson.

Three ways to repeat

A teacher adds up thirty marks. A game asks for a password until it is right. A program halves a number while it is more than one. Each one repeats steps. This is iteration. The Python Foundations chapters "Loops: Repeat with for" and "Loops: Repeat Until with while" teach it in Python. The syllabus has three loops, and the exam asks you to choose the right one.

The count-controlled loop: FOR

Use a FOR loop when you know how many times to repeat.

pseudocode
DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total ← 0
FOR Count ← 2 TO 10 STEP 2
    Total ← Total + Count
NEXT Count
OUTPUT Total

The counter Count takes each value from 2 to 10, going up by the STEP of 2. The statements inside run once for each value. So Total becomes 2+4+6+8+10=302 + 4 + 6 + 8 + 10 = 302+4+6+8+10=30, and the output is 30. Without STEP, the counter goes up by 1. The step can be negative, to count down. If the start value is already past the end value, the loop runs no times at all.

In Python, FOR Count ← 2 TO 10 STEP 2 is for count in range(2, 11, 2). The end of range() is one past the last value.

The post-condition loop: REPEAT ... UNTIL

pseudocode
DECLARE Password : STRING
REPEAT
    OUTPUT "Please enter the password"
    INPUT Password
UNTIL Password = "Secret"

The condition is tested after the statements. So the statements always run at least once, and the loop stops when the condition is TRUE. That suits a password check: you must ask at least once. Python has no REPEAT keyword. It uses while True: with break at the end.

The pre-condition loop: WHILE ... DO

pseudocode
DECLARE Number : INTEGER
INPUT Number
WHILE Number > 9 DO
    Number ← Number - 9
ENDWHILE
OUTPUT Number

The condition is tested before the statements. The loop goes on while the condition is TRUE. If it is FALSE the first time, the statements never run. With the input 40, Number becomes 31, 22, 13 and then 4, so the output is 4. With the input 7, the loop does not run, and the output is 7.

Two flowcharts side by side. Left, a WHILE loop: INPUT Number, then a diamond Number > 9 ?; Yes goes down to the rectangle Number ← Number - 9 and back up to the diamond; No goes to OUTPUT Number. Right, a REPEAT loop: INPUT Password, then a diamond Password = "Secret" ?; No goes back up above the input; Yes goes down to Stop.

The two flowcharts show the difference. In the WHILE loop the decision comes before the statements. In the REPEAT loop it comes after them.

Totalling and counting

A total starts at zero and has each new value added: Total ← Total + Temperature. A count starts at zero and goes up by one each time something happens: HotDays ← HotDays + 1. Here a weather station reads five temperatures in degrees Celsius:

pseudocode
DECLARE Count : INTEGER
DECLARE Temperature : REAL
DECLARE Total : REAL
DECLARE HotDays : INTEGER
Total ← 0.0
HotDays ← 0
FOR Count ← 1 TO 5
    INPUT Temperature
    Total ← Total + Temperature
    IF Temperature > 30.0
      THEN
        HotDays ← HotDays + 1
    ENDIF
NEXT Count
OUTPUT "Average ", Total / 5
OUTPUT "Hot days ", HotDays

The five inputs are 28.528.528.5, 31.031.031.0, 33.533.533.5, 29.029.029.0 and 30.530.530.5. Three of them are above 30.030.030.0. Their total is 152.5152.5152.5, so the average is 152.5÷5=30.5152.5 \div 5 = 30.5152.5÷5=30.5. The output is Average 30.5 and then Hot days 3. Notice the IF statement inside the FOR loop. That is a nested statement.

Trace tables

A trace table records the value of each variable, and each output, at every step of a dry run. You start a new row when a value changes. Here is the start of the trace table for the weather program.

A trace table with three columns, Count, Total and HotDays. Rows: blank, 0.0, 0; then 1, 28.5, 0; 2, 59.5, 1; 3, 93.0, 2; 4, 122.0, 2; 5, 152.5, 3.

Read it from the top. Count is 1 and the input is 28.528.528.5: the total becomes 28.528.528.5, and the count of hot days stays 0. In the second row the input 31.031.031.0 is above 30.030.030.0, so HotDays becomes 1. A trace table on a phone is easier to read with three columns at most, so the outputs are left for the end.

Nested loops

A loop can sit inside another loop. The inner loop runs all the way through for every pass of the outer loop.

pseudocode
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE Stars : INTEGER
Stars ← 0
FOR Row ← 1 TO 3
    FOR Column ← 1 TO Row
        Stars ← Stars + 1
    NEXT Column
NEXT Row
OUTPUT Stars

For Row 1 the inner loop runs once, for Row 2 twice, and for Row 3 three times. So Stars is 1+2+3=61 + 2 + 3 = 61+2+3=6. The exam does not ask you to write more than three levels of nesting.

Practise with new numbers

Try it in Python

The program has the three loops of this lesson in Python, and a nested loop that prints a small times table.

Read it first. Then run it. Check the FOR total and the WHILE result against the pseudocode above.

1. Change number = 40 to number = 9. Does the WHILE loop run at all?

2. Change the guesses to [5, 2, 7]. How many tries does the REPEAT loop need now?

Common mistakes

  • Choosing the wrong loop. A fixed number of passes is a FOR loop. A loop that must run at least once is a REPEAT loop. A loop that may never run is a WHILE loop.
  • Forgetting to set the total to zero first. Then the total starts from an old value.
  • Setting the total to zero inside the loop. Then it is wiped on every pass.
  • A condition that never changes. If nothing inside a WHILE loop changes its condition, the loop never stops.
  • A NEXT with the wrong name. FOR Count ends with NEXT Count.

Remember these

  • FOR is count-controlled. WHILE tests first and may run no times. REPEAT tests last and runs at least once.
  • REPEAT stops when its condition is TRUE. WHILE stops when its condition is FALSE.
  • A total adds each value. A count adds one each time.
  • A trace table shows every variable and output, step by step.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. How many times does FOR Index ← 10 TO 1 STEP -3 run, and what values does Index take?
  1. Which loop would you use to ask for a mark until it is between 0 and 100?
  1. With the input 25, what does the WHILE program of this lesson output?
  1. A program must count how many of 20 inputs are negative. Which two statements go inside the loop?
  1. In the star program, change TO Row to TO 3. What is the output now?

Enroll free in Variables, Selection and Loops in Pseudocode

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (4)

Ana saves money each week. This pseudocode works out her savings in dollars…Easy · High School (9–10) · by StemPy Lab
A box office sells from 1 to 6 tickets to one person…Medium · High School (9–10) · by StemPy Lab
This pseudocode works on a whole number that is input…Hard · High School (9–10) · by StemPy Lab
That was the last lessonBack to the course
← Variables, Selection and Loops in Pseudocode
← Selection with IF and CASENext →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

A long scenario question. At a sports day, six runners each run 100 metres…Hard · High School (9–10) · by StemPy Lab

Python Labs (1)

Totals and counts in a loopPython Lab · by StemPy Lab

Question Sets (1)

Variables, Selection and Loops: Check Your Understanding10 questions · by StemPy Lab