Functions and loops DSDN 142 Creative Coding
LOOPS Loops are a very quick and easy way to excecute code repeatedly. If you are wanting to do something like draw a 1000 lines you could easily write out line() 1000 times with slightly different numbers each time. This would be severly inefficient, however, and rather pointless. Using a loop, however, you could draw 1000 or even 1000000000 lines with exactly the same amount of code! An important thing to understand though is that loops deal with numbers, nothing else. You can use these numbers for anything though as the numbers are inside variables. If you haven't yet done the variables presentation, go do it now! It is essential you understand variables before trying to tackle loops! WHILE LOOPS: While loops are the bread and butter of any programmers diet. They are the most fundamental form of loop and are an easy way to achieve most repetititive task. There are two parts of a while loop: The condition and the body (the code contained by the loop). The condition of the loop is essentially what tells the loop when to stop running, if the loop never stops running your computer will crash!
while(condition goes here) { //code goes here }
This is the basic syntax for a while loop, it is very similar to a function (as you will soon see) the condition goes between the brackets and the code goes between the braces.