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:
bongSlanhOun "countdown";
akthe n = 5;
nvpeldae (n > 0) {
jongyeytha n;
n = n - 1;
}
jongyeytha "Liftoff!"; 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:
bongSlanhOun "for";
somhab (akthe i: LekKut = 1; i <= 5; i += 1) {
jongyeytha "Hello number " + i;
} Hello number 1
Hello number 2
Hello number 3
Hello number 4
Hello number 5 Inside the brackets are three parts, separated by semicolons:
- Start:
akthe i: LekKut = 1makes a counter calledi, starting at1. This happens once, at the beginning. - Keep going while:
i <= 5is checked before every iteration, just likenvpeldae. - Step:
i += 1runs 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:
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;
} 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:
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; 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:
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;
} 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.
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";
}
} 1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even Your turn
- Print the numbers from 10 to 1, then
Happy New Year!. Hint: asomhabcounting down withi -= 1. - Print the 7 times table:
7 x 1 = 7,7 x 2 = 14, up to7 x 10 = 70. Hint: one loop, and build each line with+. - Use a
nvpeldaeloop to find how many times you can double1before it's bigger than1000. Hint: keep one variable for the number and one for the count.
Solution for exercise 3:
bongSlanhOun "doubling";
akthe number: LekKut = 1;
akthe doublings: LekKut = 0;
nvpeldae (number <= 1000) {
number *= 2;
doublings += 1;
}
jongyeytha "After " + doublings + " doublings it's " + number; After 10 doublings it's 1024