ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Iteration/Range and step
J277 · 2.2.1

Unit 7 · Lesson 2

Range and step

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Use ERL step and Python range(start, stop, step) to control loop values.

A step changes the loop variable by an amount other than one. Positive steps count upwards and negative steps count downwards. ERL still includes its to value when the step reaches it. Python range(start, stop, step) still excludes stop, even with a negative step.

Step

The amount added to the loop variable after each iteration.

Ascending

Moving from a smaller value to a larger value.

Descending

Moving from a larger value to a smaller value.

Off-by-one error

A logic error caused by starting or stopping one position too early or too late.

Keep these in mind

  • ERL writes step after the inclusive end value.
  • Python writes the step as the third range argument.
  • A descending range needs a negative step.
  • To include zero in a Python countdown, use a stop below zero.

Learning objective

Use ERL step and Python range(start, stop, step) to control loop values.

Exam tip

For a countdown from 10 to 0, translate ERL step -1 to Python range(10, -1, -1).

Common mistake

range(5, 0) produces no values because its default step is +1 and cannot move from 5 towards 0.

PreviousNext