Learn/Nim/5. Loops
Lesson 5 of 8
Sign in

Nim, lesson 5

Loops

Computers are great at doing the same thing many times. Nim has two loops: for, which walks over a sequence of values, and while, which repeats as long as a condition holds.

for over a range

1..5 is a range that includes both ends, so this prints laps 1 to 5. A few variations:

The loop variable (i here) is created by the loop and only exists inside it. You cannot change it inside the body; Nim treats it as read-only.

for over things

A for loop can walk over anything Nim knows how to iterate: the characters of a string, the items of a list, or the lines of input.

stdin.lines gives you one line at a time (without the line break) and simply stops when the input runs out, so there is no end-of-file error to worry about.

2 exercises to try what you just read.

Exercise 1

Times table

Press Ctrl Enter or Check to test your code.

Exercise 2

Savings goal

Press Ctrl Enter or Check to test your code.

while

A while loop checks its condition before every pass. Use it when you don't know in advance how many passes you need:

Make sure something in the body moves the condition towards false, or the loop never ends.

Leaving early: break and continue

  • break jumps out of the loop immediately.
  • continue skips the rest of this pass and starts the next one.

A common pattern is a for loop over input that breaks as soon as it has found what it needs, leaving the rest of the input unread.

Further reading

  • Nim Tutorial (Part I): while, for and break statements
  • Nim Manual: iterators and the for statement
  • The syncio module (lines)

Read a whole number n and print its times table from 1 to 10, one line each, in this form:

Input: one line with n (it may be 0 or negative). Output: exactly ten lines.

You are saving up for a bike. Read your savings goal, then a list of deposits. Count how many deposits it takes for the total to reach the goal (equal counts as reached). Stop reading deposits as soon as you get there.

Input: the first line is the goal (at least 1). Each following line is one deposit (a whole number of 0 or more). There may be no deposits at all.

Output: if the goal is reached, print Goal reached! Deposits used: K. Otherwise print Short by X. where X is how much is still missing.

prints Goal reached! Deposits used: 3

for i in 1..5:
  echo "Lap ", i
for i in 0..<3: echo i            # 0, 1, 2 (stops before 3)
for i in countdown(5, 1): echo i  # 5, 4, 3, 2, 1
for i in countup(0, 10, 5): echo i # 0, 5, 10 (step of 5)
for ch in "Nim":
  echo ch           # N, i, m

for line in stdin.lines:
  echo "You typed: ", line
var balance = 100
var years = 0
while balance < 200:
  balance = balance + balance div 10
  years += 1
echo "Doubled after ", years, " years"
for n in 1..100:
  if n mod 2 == 1:
    continue         # skip odd numbers
  if n > 10:
    break            # stop completely after 10
  echo n             # 2, 4, 6, 8, 10
7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70
100
30
50
40
10