Repeats a sequence of code a specified number of times, and tracks a value each time the loop is run.
Sometimes we want to repeat things a certain number of times, but we want to keep track of values as we do. For loops are different than repeat loops because they use a counter variable to track a value. That value can then be used in the code inside the block.
The counter variable begins with the starting value. Each time the program goes through the loop, the sequence of code inside the block is run. The step value is then added to the counter variable, and the loop continues to run until the counter reaches the stopping value. Once the for loop is finished, the program will continue where it left off.
for counter from 1 to 4 count by 1
The loop will run 4 times.
counter = 1
counter = 2
counter = 3
counter = 4
for counter from 3 to 9 count by 3
The loop will run 3 times.
counter = 3
counter = 6
counter = 9
|variable||The name of the variable that keeps track of the number that controls the loop. Default name is “counter”|
|starting value||The value the counter variable start at|
|ending value||The value the counter variable must reach before the loop will end|
|step value||The value to add to the counter variable each time the loop is run|
Found a bug in the documentation? Let us know at firstname.lastname@example.org