ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Iteration/Counters and accumulators
J277 · 2.2.1

Unit 7 · Lesson 5

Counters and accumulators

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Initialise and update counters and accumulators correctly around a loop.

A counter records how many matching events occur; an accumulator combines values into a running total. Both usually start at zero before the loop. Inside the loop, update them using their previous value, such as count = count + 1 or total = total + score.

Counter

A variable updated to record how many times an event has occurred.

Accumulator

A variable that stores a running total or combined result.

Initialise

Give a variable its starting value before it is first used.

Increment

Increase a stored number and save the new value.

Keep these in mind

  • Initialise counters and totals before entering the loop.
  • A counter normally adds one when a condition is met.
  • An accumulator adds the current value to the total already stored.
  • The update assignment must store the result back into the counter or accumulator.

Learning objective

Initialise and update counters and accumulators correctly around a loop.

Exam tip

Keep the initial value outside the loop and the update inside. Trace the variable after every update.

Common mistake

total = 0 inside the loop erases the previous total every iteration. Also, count =+ 1 assigns positive one instead of incrementing.

PreviousNext