Category: Control
Executes a block of statements a certain number of times depending on the initialization expression, conditional expression, and increment expression.
Instead of typing a block of statements again and again, you can use a for loop around the block of statements. The most common usage of a for loop is simply to use it as a counting loop to execute a block of code a certain number of times.
// Draw 4 dots along a line.
for(var i=0; i<4; i++){
dot(5);
moveForward();
}
Example: Count Down Count down to zero from ten using a negative increment.
// Count down to zero from 10.
for(var i=10; i>0; i--){
write(i) ;
}
write('Blast Off!');
Example: One Inch Draw 8 tic marks along a line.
penUp(); turnLeft(); moveForward(100); turnLeft(180); penDown(); for(var i=0; i<7; i++){ ticMark(); moveForward(); } ticMark(); penUp(); moveForward(); function ticMark() { turnLeft(); moveForward(5); turnLeft(180); moveForward(5); turnLeft(); }
Example: Rake Draw a rake ranging the angles from -45 to 45 by 5s.
// Draw a rake ranging the angles from -45 to 45 by 5s.
for(var angle=-45; angle<=45; angle=angle+5){
turnTo(angle);
moveForward(100);
turnLeft(180);
moveForward(100);
}
turnTo(180);
moveForward(200);
Example: Flower Draw a flower with a parameter number of petals. Works best for petalCount between 5 and 10.
// Draw a flower with a parameter number of petals. Works best for petalCount between 5 and 10.
drawFlower(5);
function drawFlower(petalCount) {
penColor("pink");
penUp();
for(var i=0; i<360; i=i+(360/petalCount)){
turnTo(i);
moveForward(360/petalCount);
dot((2/3)*360/petalCount);
moveForward(-360/petalCount);
}
penColor("blue");
dot(360/petalCount/2);
}
|
|
Example: Random Die Rolls Simulate rolling a die using a random number from 1 to 6, and roll the die 10000 times to check if the expected roll is 3.5.
// Simulate rolling a die using a random number from 1 to 6, and roll the die 10000 times to check if the expected roll is 3.5.
var sum = 0;
for (var i = 0; i < 10000; i++) {
sum = sum + randomNumber(1,6);
}
console.log(sum/10000);
for (initialization; condition; increment) {
// block of statements
}
Here is a typical construct for loop used to count from 0 to 3 to execute the block of code 4 times:
for(var i = 0; i < 4; i++)
initialization var i = 0; is executed once, before anything else. Create an identifier named i and initialize it to 0.
condition i < 4; is checked before each iteration, to see if the block of statements should execute or not. If i is less than 4.
increment i++ is executed after every iteration, after the block of statements is executed. Increase (increment) i by 1.
Found a bug in the documentation? Let us know at documentation@code.org
for(var i=0; i<4; i++){ //code }
Found a bug in the documentation? Let us know at documentation@code.org