This guide will teach you how "for" loops and "while" loops are used in the Touch Develop code.

You will encounter situations where you will need to execute a block of code repeatedly. Instead of typing up the same sequence of code again and again, you can use loops.

In general, statements are executed sequentially: the first statement in a program is executed first, followed by the second, and so on. However, with a loop statement, we can execute a statement or sequence of statements multiple times.


3.a. "For" loops

3.b. "While" loops


Exercises

Now use the things you have learned in order to solve more difficult exercises.

  1. Create a script using a "for" loop that uses an index which counts from 0 to 10 (including 10). For each iteration of the for" loop, multiply the index by 9 and then print out the result.
  2. Create a script using a While Loop that will print out the following result: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5


Don't stop here! Experiment with the commands you have learned and create scripts of your own.



PREVIOUS LESSON

NEXT LESSON

BACK TO INDEX