Blockland Forums > Modification Help
Why won't this work?
Slicksilver:
--- Quote from: Brian Smithers on January 22, 2012, 11:55:43 PM ---function test(%y)
{
for(%i=0;%i<%y;%i++)
echo(%y);
}
--- End quote ---
his example is stuff, but you have to insert the y variable. test(10); would say ten 10 times, becaus he made it echo %y instead of %i. in that scenario it would be the same as:
for(%i = 0; %i < 10; %i++)
{
echo(10);
}
for statements work like such
for(%variable = value; %variable (less than, more than, equal to, not equal to, etc) number; modify %variable)
inside the {}s for a for statement is repeated until the condition in the middle is met, in this case until %i is as big as 10.
for(i is zero, i is less than 10, increase i by one)
{
echo(i)
}
CityRPG:
A for loop is a simplified while loop.
--- Code: ---%var = 0;
while(%var < 100) {
echo("Loop #" @ %var);
%var++;
}
--- End code ---
This was very standard practice. Since for loops were conceived, this extremely common loop type can be reduced to:
--- Code: ---for(%var = 0; %var < 100; %var++) {
echo("Loop #" @ %var);
}
--- End code ---
You can put anything you want in it, though.
--- Code: ---for(openDoor(); getOnFloor(); walkDinosaur()) { }
--- End code ---
That code will call openDoor(), check to see if getOnFloor() returns a boolean value of true, and for every time it does, calls walkDinosaur().
Think I'm kidding? Run this code.
--- Code: ---function openDoor() { echo("Open the door!"); }
function getOnFloor() { echo("Get on the floor!"); return getRandom(0, 10) != 10; }
function walkDinosaur() { echo("Everybody walk the dinosaur!"); }
for(openDoor(); getOnFloor(); walkDinosaur()) { }
--- End code ---
Output:
--- Code: ---Open the door!
Get on the floor!
Everybody walk the dinosaur!
Get on the floor!
Everybody walk the dinosaur!
Get on the floor!
Everybody walk the dinosaur!
Get on the floor!
--- End code ---