🌱 Beginner's Guide lesson 5 of 12

Loops

Imagine you had to print the numbers from 1 to 100. You could write 100 jongyeytha lines... or you could tell the computer to repeat one line 100 times. That's what a loop is: a block of code that runs again and again. Repeating boring things is what computers are best at.

nvpeldae: while

nvpeldae means while. It works like ber, except that after running its block it goes back and asks the question again, and keeps going for as long as the answer is ok:

[ try-it.sts ]
bongSlanhOun "countdown";
akthe n = 5;
nvpeldae (n > 0) {
    jongyeytha n;
    n = n - 1;
}
jongyeytha "Liftoff!";
[ what it prints ]
5
4
3
2
1
Liftoff!

Let's follow it step by step. n starts at 5. Is 5 > 0? Yes, so print 5 and make n one smaller. Back to the top: is 4 > 0? Yes... and so on, until n is 0. Is 0 > 0? No! So the loop stops, and the program carries on after the block.

Each trip through the block is called an iteration.

Watch out for endless loops

What if we forgot the n = n - 1; line? Then n would stay 5 forever, 5 > 0 would always be ok, and the loop would never end. This is called an infinite loop, and every programmer writes one sooner or later.

If it happens to you, don't panic: press Stop, or press Ctrl+C in the terminal, and the playground will stop your program.

somhab: for

Counting with a loop is so common that there's a special loop for it. somhab means for, and it packs the three parts of counting into one line:

[ try-it.sts ]
bongSlanhOun "for";
somhab (akthe i: LekKut = 1; i <= 5; i += 1) {
    jongyeytha "Hello number " + i;
}
[ what it prints ]
Hello number 1
Hello number 2
Hello number 3
Hello number 4
Hello number 5

Inside the brackets are three parts, separated by semicolons:

  1. Start: akthe i: LekKut = 1 makes a counter called i, starting at 1. This happens once, at the beginning.
  2. Keep going while: i <= 5 is checked before every iteration, just like nvpeldae.
  3. Step: i += 1 runs after every iteration, moving the counter along.

The name i is a tradition (short for "index"), but you can call it anything. We make it a LekKut because counters count in whole numbers, and it'll matter later when we use counters with lists.

You can count in steps other than one, or even backwards:

[ try-it.sts ]
bongSlanhOun "steps";
somhab (akthe i: LekKut = 0; i <= 20; i += 5) {
    jongyeytha i;
}
somhab (akthe i: LekKut = 3; i >= 1; i -= 1) {
    jongyeytha "back " + i;
}
[ what it prints ]
0
5
10
15
20
back 3
back 2
back 1

Adding things up

A classic loop job is keeping a running total. Here's the sum of all the numbers from 1 to 100:

[ try-it.sts ]
bongSlanhOun "sum";
akthe total: LekKut = 0;
somhab (akthe i: LekKut = 1; i <= 100; i += 1) {
    total += i;
}
jongyeytha "The sum of 1 to 100 is " + total;
[ what it prints ]
The sum of 1 to 100 is 5050

The variable total is made before the loop, so it survives from one iteration to the next. Each time around, the current i gets added to it. (Legend says the mathematician Gauss worked out 5050 in his head as a schoolboy. You just did it with a computer, which counts too.)

Loops inside loops

You can put a loop inside another loop. The inner loop runs completely, from start to finish, for every single iteration of the outer loop. That's perfect for a times table:

[ try-it.sts ]
bongSlanhOun "times table";
somhab (akthe row: LekKut = 1; row <= 4; row += 1) {
    akthe line = "";
    somhab (akthe col: LekKut = 1; col <= 4; col += 1) {
        line = line + "  " + row * col;
    }
    jongyeytha line;
}
[ what it prints ]
  1  2  3  4
  2  4  6  8
  3  6  9  12
  4  8  12  16

For each row, we start with an empty string "" and the inner loop adds four numbers onto it. Only then do we print the finished line.

Mixing loops and decisions

Loops and ber go really well together. steav2 doesn't have a "remainder" operator, but with whole numbers you can work out whether a number is even: divide by 2 (which throws away the remainder), multiply back, and see if you got the same number.

[ try-it.sts ]
bongSlanhOun "even";
somhab (akthe n: LekKut = 1; n <= 6; n += 1) {
    ber ((n / 2) * 2 == n) {
        jongyeytha n + " is even";
    } minjengte {
        jongyeytha n + " is odd";
    }
}
[ what it prints ]
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even

Your turn

  1. Print the numbers from 10 to 1, then Happy New Year!. Hint: a somhab counting down with i -= 1.
  2. Print the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 10 = 70. Hint: one loop, and build each line with +.
  3. Use a nvpeldae loop to find how many times you can double 1 before it's bigger than 1000. Hint: keep one variable for the number and one for the count.

Solution for exercise 3:

[ try-it.sts ]
bongSlanhOun "doubling";
akthe number: LekKut = 1;
akthe doublings: LekKut = 0;
nvpeldae (number <= 1000) {
    number *= 2;
    doublings += 1;
}
jongyeytha "After " + doublings + " doublings it's " + number;
[ what it prints ]
After 10 doublings it's 1024
  • steav2♡ help
  • Y2K✓ ready
  • wasminside
  • .stsvalid

best viewed at 1024x768 ☆ made with ♡ ☆ the reference is steav2's own docs