0% found this document useful (0 votes)
9 views

05_For_Loops_96bb6b20d38

The document provides an overview of 'For Loops' in programming, emphasizing their utility in executing repetitive tasks efficiently by reducing the amount of code needed. It includes examples in Python and JavaScript, illustrating how to implement 'For Loops' and the importance of using distinct variable names in nested loops to avoid errors. Additionally, it presents an activity that encourages students to apply their understanding by writing a program to display numbers from 1 to 1000.

Uploaded by

panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

05_For_Loops_96bb6b20d38

The document provides an overview of 'For Loops' in programming, emphasizing their utility in executing repetitive tasks efficiently by reducing the amount of code needed. It includes examples in Python and JavaScript, illustrating how to implement 'For Loops' and the importance of using distinct variable names in nested loops to avoid errors. Additionally, it presents an activity that encourages students to apply their understanding by writing a program to display numbers from 1 to 1000.

Uploaded by

panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Thus we can reach a solution within seconds just by writing a short piece of code.

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

Repeating Why is it wrong? Why is it wrong? Correct form:


commands
You should write the We must space Our code is correct due to
commands you want between the paying attention to
to repeat within for expressions we write spacing, which we need to
loop after indenting within loop. Such as leave between expressions
with tab key. “For a” and“in and to the indenting which
range”. is required to be done with
the tab key.
PYTHON
Usage Explanation
JAVASCRIPT
Variable Number of iterations Usage Explanation
Variable

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

Usage of Nested For Loop


Block Python Javascript
As you see below we have two repetitive tasks. Two nested for loops can be used
for solution in this case and in such cases. It is important to differentiate variable
names, which we will give as initial value while writing our nested loops for
ensuring correct execution of the task solution.

Nested For Loop


Block Explanation
Sample usage (Python):

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

Algorithm Block Python Javascript

 Start for a in range(2): for(var a=0;a<2;a++){


 2 times within For loop forward(3) forward(2)
 Move up forwardUp() turn(left)
 Move down forwardDown() forward(2)
 Move 1 square forward forward(1) turn(right)
}

codementum.com

You might also like