Skip to content
NorscodeNorscode

Make your first loop in Norscode

ArticleBy the Norscode project

Loops let code repeat. Learn mens and for with small, runnable examples.

How a loop repeats la i = 0 i < 5 ? true skriv(i) i = i + 1 repeat false → done

A loop is how you get code to repeat something without writing the same thing over and over. It is one of the first things that makes programming powerful — and in Norscode, loops read almost like ordinary Norwegian.

mens: repeat as long as something is true

The most basic loop is mens. It repeats as long as the condition holds:

funksjon start() -> heiltall {
    la i = 0
    mens i < 5 {
        skriv(tekst(i))
        i = i + 1
    }
    returner 0
}

This prints the numbers 0 to 4. Notice i = i + 1: without it, the condition would always be true, and the loop would never stop. If you forget to move toward the end, you get an infinite loop.

Go through a list

Often you want to do something with each element in a list. You can do that with mens and a counter:

la frukt = ["eple", "pære", "plomme"]
la i = 0
mens i < lengde(frukt) {
    skriv(frukt[i])
    i = i + 1
}

lengde(frukt) gives the number of elements, and frukt[i] fetches element number i.

for: the simpler way

When you just need to visit each element, there is a shorter form with for:

for f i frukt {
    skriv(f)
}

Here you skip the counter entirely — f becomes each element in turn. Use for when you go through a whole list, and mens when you need more control over when the loop should stop.

Try it yourself

Save one of the examples in lokke.no and run:

nc run lokke.no

A little exercise

Try changing the first loop so that it prints the even numbers 0, 2, 4, 6, 8, and 10. Hint: count with i = i + 2 instead of i = i + 1, and let the loop run as long as i <= 10. Trying things out is exactly how you learn loops.

Once loops stick, move on with Getting started, or browse the examples.

Count downward

A loop can just as well count the other way. Here we count from 5 down to 1:

la i = 5
mens i > 0 {
    skriv(tekst(i))
    i = i - 1
}

The only things that differ are the starting value, the condition, and that we subtract instead of add. The principle is the same: do something, move toward the end, repeat.

Loops inside loops

You can have a loop inside another. This prints a small multiplication table:

la rad = 1
mens rad <= 3 {
    la kol = 1
    mens kol <= 3 {
        skriv(tekst(rad * kol))
        kol = kol + 1
    }
    rad = rad + 1
}

The outer loop runs once per row, and the inner one runs all the way through for each of them. Nested loops are powerful, but remember that the work grows quickly — two loops that each run a thousand times become a million steps.

Break and continue

Sometimes you want out of a loop before the condition says stop, or to skip one round. For that, mens has two words: bryt ends the loop immediately, and fortsett jumps to the next round.

la i = 0
mens i < 10 {
    i = i + 1
    hvis i == 5 { fortsett }
    hvis i == 8 { bryt }
    skriv(tekst(i))
}

This prints 1, 2, 3, 4, 6, and 7 — it skips 5 and stops completely at 8.

Common mistakes with loops

  • Infinite loop: if you forget to move toward the end (i = i + 1), the condition never becomes false, and the program hangs. If you see something running without stopping, this is the most common cause.
  • One too many or one too few: the difference between < and <= decides whether the last value is included. Always test that the loop hits exactly the numbers you expect.

Keep practicing

Try writing a loop that adds up the numbers from 1 to 100, or one that only prints the even numbers. Loops are one of those things that stick best when you have written a few yourself. After that, the examples are full of loops in real use.

Related

Back to the overview