Iterate from 10 down to 1 using a descending for-range.

← Control Flow · Ref: Q1148

EK9 auto-detects descending when start > end:

  for i in 10 ... 1
    sum: sum + i

No explicit step needed. For custom steps use 'by': for i in 10 ... 1 by 2. See Q1149.

Example

defines module qa.controlflow.forrangedescending

  defines program

    ForRangeDescendingDemo()
      stdout <- Stdout()
      sum <- 0

      //Descending: 10 down to 1 automatically
      for i in 10 ... 1
        sum: sum + i

      //Sum of 10+9+8+...+1 = 55
      stdout.println(`Sum: ${sum}`)

Common mistakes

E50001 — EK9 uses 'for i in start ... end' with three dots. No range() function. Descending is automatic when start > end.

Incorrect:

      for i in range(10, 0, -1)

Correct:

      for i in 10 ... 1
Other ways to ask this
  • I need a countdown loop from 10 to 1
  • In Python I'd use range(10, 0, -1). Write the EK9 descending range
  • Given start=10 and end=1, iterate downward automatically
  • Loop in reverse order over a numeric range

Coming from another language?

Python: range(10, 0, -1). Java: for(int i=10; i>=1; i--). Go: for i := 10; i >= 1; i--. EK9: for i in 10 ... 1 — automatic descending.

Keywords: countdown, loop, reverse, range, for, descending