05_For_Loops_96bb6b20d38
05_For_Loops_96bb6b20d38
Subject: For Loops Now let's continue to our document for a better understanding of our subject.
Overview Commands to be Used
One of the most basic structures of programming is loops. We can execute for loop which will be used for our character to complete repetitive tasks and the
the repeating actions more systematically by using loops. Loops also enable us to commands which were learnt before.
create less code.
Blocks and Button icons to be used in the training about this subject
In this lesson, your students will learn the “For Loop” which they must use
for completing the repetitive actions in the short way for our task scenes. "For"
allows us to repeat the actions we write within the loop by the offset value Block Explanation
entered. This enables us to complete the task in a short way by writing every step We can easily write repetitive actions which we
once within the loop instead of writing every step one by one in our tasks that are need to write for completing our tasks thanks to
repeated multiple times. for loop, which allows us to specify the actions we
want to repeat and how many times we want
In our lesson, we will show your students the writing form of "For Loop"
them to repeat. The commands we write within for
which has a similar usage with “repeat n:” command we learnt in our previous loop repeat by the number we specified as the
lessons. This means they can solve our tasks belonging to our lesson efficiently. Button interval.
We generally use for loop in situations where we know the number of
Sample usage (Python):
loops. In for loop, we can also define start, finish and increase/decrease values of
for a in range(2):
our counter in our loop. forward(1)
turn(left)
Activity
forward(1)
In our activity you want to write the numbers from 1 to 1000 on the screen. turn(right)
How would you do that?
Sample usage (Javascript):
Would you continue by writing all numbers till 1000 one by one? Let's
for(var a=0;a<2;a++){
suppose that you continued like that. You wrote all the 1000 numbers on the forward(1)
screen one by one and probably spent most of your time on this action. turn(left)
Or would you like to write it at once by writing an efficient code as below? forward(1)
turn(right)
for a in range(1000) }
print(a)
a=a+1
codementum.com
BLOCK Erronous writing forms
Usage Explanation
Python
Variable
Number of iterations
Number of iterations
Repeating
commands
Repeating
commands
mber of
iterationsNu
mber of
iterations
Number of codementum.com
iterationsNu
mber of
Erronous writing forms
WARNING
JavaScript It is important to differentiate the names of initial variables, "for" will be used in
situations where we want our characters to move according to the loop by using
nested "for" ensuring correct execution of the task solution. As in the example
above, in nested for loop, since the first loop will run 3 times, and the loop within it
will run 5 times, the loop within it will work 15 (3x5=15) times. Otherwise, if we use
the same variable name within the two loops, it would run 5 times instead of 15 in
total. To set an example:
Erronous writing forms
WARNING
Sample usage (Javascript): Normally if two identical variable names were not used, our command block would
run 15 times in total, but because here "a '' is used for both loops, it will just run 5
times. So we should write different variable names in nested for to avoid such
situations.
codementum.com
Sample Solution: Scene 43
codementum.com