Blockland Forums > Modification Help

Can anyone explain to me what "for(%i = 0; %i < 4; %i++)" is?

Pages: (1/2) > >>

Bloxxed:

And incase you didn't see, I want for(%i = 0; %i < 4; %i++) explaining.

Can someone explain to me in steps why it is used, what it is used for, and why it is so important?

Thanks.

Plornt:

It creates a loop. It puts %i at 0 and while %i < 4 is true it will do the stuf inside the brackets. And %i++ means it will add 1 to %i in each loop.

Iban:

for(%i = 0; %i < 4; %i++)

is the exact same thing as this:

%i = 0;

while(%i < 4)
{
   %i++;
}

This loop will run 4 times, because the starting step is 0 (what we declare %i as), and the loop terminates when %i reaches 4. Meaning, %i will not run when %i is 4, so %i will run when %i is equal to: 0, 1, 2, or 3. That is 4 steps.

If you used the <= operator, it will run while %i is less than or equal to 4, which adds 4 to the list, which makes it 5 steps.



Bloxxed:

Thanks, also, may I also request some more help?

I've been scripting Client sided scripts for a while, and I want to script something server sided, or something simple serversided wise, any idea's other than kill scripts, etc?

DrenDran:

A bit more wide than Iban's definition...
"for" is just the keyword, then come three 'statements' in side parenthensies.
The first one is a statement which will run when the statement is first called.
If the second one returns a value greater than zero, then the next statement will execute, along with anything in brackets after it.

So you could do:

--- Code: ---$myvar = 0;
function myvar_increase()
{
$myvar++;
}
function myvar_lessthannum(%a)
{
if($myvar < %a)
return true;
else
return false;
}

for(1;myvar_lessthannum(10);myvar_increase())echo("Hey");

--- End code ---
And it would work, or at least, in c++ it would, should be the same in torque.

Edit: yes it does, tested it, but you need to actually put something before the first semicolen, unlike in c++ where a ";" alone constitutes a valid command

Pages: (1/2) > >>

Go to full version