13. The for loop
Hi, In the previous lesson we learned and use the repeat loop. Now we will learn how to use another loop the for loop. Please clean the screen
The for definition is as follow. for [var_name var_initial_value var_last_value advance] [command sequence ]. Let's start by showing an example. for [i 1 10 1] [print :i] .Our var_name here is i his initial value set to 1. we basically say to the sequence of command till i>10 , for each sequence the value of i will increase in 1(the third number). First time i=1 we are printing his value so 1 is printed . in the second time i=2 (why?) Write down the command for [i 1 10 1] [print :i]
just look at the output
for [i 1 10 1] [print :i]
Now we will try to use the For command to write the even number between 0-10. we should think. what should be the variable initial value? . how much should we advance in each step? when should the loop stop? Please print the even numbers between 0-10 ( the variable in the for loop will be called i)
The initial value will be 0 , we will advance i variable 2 each time , and print his value
for [i 0 10 2] [print :i]
Lets create a spiral. For creating a spiral we will go 10 points forward and then turn right . then we will go 20 points forward and turn right , then 30 point and turn right .. till 100 point Lets use the for loop to create a Spiral , I initial value will be 10 the jumping will be of 10 till i =100.
Use shortcut command.
for [i 10 100 10] [fd :i rt 90]
In the loops lesson we have learn how to create a square , repeat 4 [ fd some_value lt 90] . Now lets use for loop to create 5 different squares , the first square edge size will be 20 , the second 40 , third 60 , forth 80 and the last 100. Create the 5 square requested in the explanation Using repeat loop inside of a for loop ( using turn left)
The sequence of command will be : repeat 4 [fd :i lt 90]
for [i 20 100 20] [repeat 4 [fd :i lt 90]]
Now we will create something for fun . Just run the command and think how was it done notice .. if no value is provided to the 'advance' value it will automatically increase by 1. run the following : for [i 1 100] [fd :i * 10 rt :i]
for [i 1 100] [fd :i * 10 rt :i]