Author Topic: Can anyone explain to me what "for(%i = 0; %i < 4; %i++)" is?  (Read 941 times)

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.

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.

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.


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?

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: [Select]
$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");
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
« Last Edit: April 14, 2011, 06:54:08 PM by DrenDran »

Obviously, but for the scope of what Bloxxed is trying to understand, purging the depths of for's technicalities is an unnecessary 1-up.

Obviously, but for the scope of what Bloxxed is trying to understand, purging the depths of for's technicalities is an unnecessary 1-up.

Still, it's nice to teach people the full functionality of something, rather than have a group of scripters who solidly believe that the "for" statement must define a variable in the first statement, must have a conditional in the second, and must have a simple variable change in the third.

Though I get what you mean by scope.

Thanks for the 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?

Turn bottomprintall, centerprintall, and announce into /commands.