Interaction Design WikiCreative Coding

Loops (en)

deutsche Version

A loop is a control statement that allows us to repeat a code block. Loops contain a conditional which determines how many times the code block is repeated.

There are few different loop types:


For-loop

for([variable declaration],[condition],[step]) {
}

for(int i=0; i <= 100; i++)
{
   println(i);
}

While-loop

while([condition]) {
}

int x=0;
while(x <= 100)
{
   println(x);
   x++;
}

Do-While-loop

do{
} while([condition]);

This is the same as the while loop, but we check the condition at the end of the code block. The do-while-loop will always execute at least once, no matter the condition. 

int x=0;
do
{
   println(x);
   x++;
}while(x <= 100);

Exercise 6

Write a program that creates a line of animated icons on the screen using a loop. Make use of the sin() function to create a simple oscillation.