איטרציה: FOR (לולאה)
FOR counts
- A count-controlled loop runs once for each integer from the start to the end, inclusive.
- The counter is an
INTEGER. - It is good practice to repeat the counter's name after
NEXT.
DECLARE Total : INTEGER
DECLARE I : INTEGER
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
עקוב אחר הסכום הנערך
לולאת FOR חוזרת על מספר קבוע פעמים. צעד שלב אחר שלב וצפה כיצד Total גדל.
STEP changes the jump
STEPsets the gap between values. It can be negative.- If the start is already past the end in the step's direction, the body does not run.
DECLARE Minute : INTEGER
FOR Minute ← 0 TO 60 STEP 15
OUTPUT Minute, " minutes"
NEXT Minute
Watch out
NEXT Imust name the counter of this loop.- The body is indented.
NEXTlines up withFOR.
Common mistakes
Before a loop, distinguish DECLARE Count : INTEGER (choose a type) from Count ← 0 (store a value). The loop header changes the counter; the body changes the result you are computing.
1 TO 5includes both 1 and 5.- Do not change the counter yourself inside the body.
Now you try
- Use a
FORloop to print a run of numbers.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
השתמש בלולאת FOR כדי להדפיס את המספרים מ-1 ועד 5, מספר אחד בכל שורה.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
הדפס 5, 3 ו1, תוך שימוש בFOR עם STEP -2.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
קרא UpperLimit (1 עד 10). הפלט את סכום הריבועים של מספרים שלמים מ-1 עד UpperLimit כולל.
לחץ על הרץ כדי לראות את התוצא כאן.