Night 7: Count the stars
You already finished this night, in Python or pseudocode. It counts for both. Practise again if you like.
Tonight: count the stars
- It is your pet's first week! Tonight it counts the stars.
- You will learn one thing: the
FORloop.
Words for tonight
| Word | Meaning |
|---|---|
| loop | do the same thing again and again |
| repeat | do again |
| counter | the variable that counts in a loop: 1, 2, 3 … |
Repeat without typing again
- Five
OUTPUTlines is a lot of typing. A loop does it for you. - The lines between
FORandNEXTrun again for each number.
DECLARE Count : INTEGER
FOR Count ← 1 TO 5
OUTPUT "Star number ", Count
NEXT Count
How FOR counts
| You write | Count is |
|---|---|
FOR Count ← 1 TO 5 |
1 2 3 4 5 |
FOR Count ← 0 TO 2 |
0 1 2 |
FOR Count ← 2 TO 10 STEP 2 |
2 4 6 8 10 |
FORcounts up to and including the last number.NEXT Countends the loop. Use the same counter name.

Loop through an array
- Use the counter as the index. Each time, the loop looks at the next element.
DECLARE Snacks : ARRAY[1:3] OF STRING
DECLARE Index : INTEGER
Snacks[1] ← "apple"
Snacks[2] ← "rice"
Snacks[3] ← "fish"
FOR Index ← 1 TO 3
OUTPUT "Yum, ", Snacks[Index]
NEXT Index
Inside or after the loop?
- Lines between
FORandNEXTare inside the loop. They repeat. - Lines after
NEXTcome after the loop. They run once.
DECLARE Count : INTEGER
FOR Count ← 1 TO 3
OUTPUT "Zzz"
NEXT Count
OUTPUT "Good night!"
Week 1 is done!
- You know
OUTPUT, variables,INPUT, numbers,IF, arrays and loops. - That is the base of almost every exam algorithm.
- Finish the tasks below and your pet grows up a stage.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Your pet counts stars. Use a FOR loop to output the numbers 1 ไป 5, one per line.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Use a FOR loop to output I like and each snack in the array. The first line is I like apple.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read a number with INPUT Times. Output Zzz that many times, one per line. Then output Good night! once, at the end.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Your pet saw stars on 4 nights. The array Stars มีไว้ 3, 5, 2 และ 6. Use a loop to add them all up in Total. Then output Total. The output is 16.
คลิก Run เพื่อดูผลลัพธ์ที่นี่