Author Topic: for statements?  (Read 1913 times)

I never understood for statements for some reason. I know they're used for loops and other things, but I don't know the syntax of it and when you use it.
Someone elaborate please? Give examples?
Thanks.

Mostly used to do something to all clients.
for(%i=0; %i < clientGroup.getCount(); %i++)
For the amount of people online, start at 0 and go up 1 every time. So inside the loop you can use the %i variable to get that client.
clientGroup.getObject(%i);

You can look in any number of add-ons for examples of for loops.

The syntax of a for loop is basically

for(start; condition; iteration)
{
  body;
}

start is done once to begin the for loop
condition is checked at the start of each loop
iteration is done at the end of every loop

Example:
Code: [Select]
for(%a=0; %a<10; %a = %a + 1)
{
     echo(%a);
}

This will count from 0 to 9, then when %a is equal to ten it will exit the loop.

The parts of a for loop do not need to be variables.
If you want to do a file search:

Code: [Select]
%path = "saves/";
for(%a = findFirstFile(%path @ "*.bls"); %a!$=""; %a = findNextFile(%path @ "*.bls"))
{
    echo(%a);
}

This will list all .bls files in your saves folder.

You can read this: http://tsforblockland.weebly.com/iterations--loops.html

It should help you understand how for loops work.

Mostly used to do something to all clients.
This is a common use, but I wouldn't say "mostly"






Also, stop locking coding help threads

Thank you
I won't lock anymore

I still don't grasp this fully.
Can someone give an example for finding who's an admin and who isn't?
Also, how can I make it only loop if the client who calls it has a variable set to true?
Maybe some other examples?
I understand what it does, but the syntax is giving me a headache.
Come on TS, you can't be easier?
« Last Edit: April 29, 2015, 04:31:50 PM by -Setro- »

I still don't grasp this fully.
Can someone give an example for finding who's an admin and who isn't?
Also, how can I make it only loop if the client who calls it has a variable set to true?
Maybe some other examples?
I understand what it does, but the syntax is giving me a headache.
Come on TS, you can't be easier?
Code: [Select]
if(%callingClient.isSmart)
{
      %count = clientGroup.getCount();

      for(%i = 0; %i < %count; %i++)
      {
            %c = clientGroup.getObject(%i);

            if(%c.isAdmin) //This person is an admin.
            {
                  %c.player.kill(); //Let's kill them for it.
            }

            else //They're not an admin.
            {
                  %c.centerPrint("Loser! You're not an admin.", 3); //Let's mock them for not being an admin.
            }
      }
}


Come on TS, you can't be easier?
This syntax is actually very common amongst C-like languages. The general syntax is for(expression; expression; expression) { } and whatever expression you put in the 3 spots, it will be syntactically valid. For example, for("this"; "language"; "sucks") {} is perfectly valid and won't even result in an infiniteloop. (Though I'm not sure if this works in other languages, which may have stricter restrictions on that sort of thing)

The thing to note is that a for loop is really just a condensed while loop, where, for example, for(%variable = 5; %variable < 100; %variable += 7) { echo(%variable); } is expanded into

%variable = 5;
while(%variable < 100)
{
    echo(%variable);

    %variable += 7;
}

What if I don't want an iteration?

Then put %b = 1 and never use %b in the code or just use a while() { };

What if I don't want an iteration?

You can do for(%x=0; %x < 10; %x) { ... } , but at this point you should ask whether a while loop would be better suited.

Also, if you want to start at a different point depending on some code further up in the script, you can do
Code: [Select]
if(something) {
        %x = 10;
} else {
        %x = -1;
}

for(%x; %x < 20; %x++) {
        doStuff();
}

What if I don't want an iteration?
I'm not sure what you mean by this... do you just want to say, call a function repeatedly? If you wanted to call it %x times then you could still use a for loop:

for(%a = 0; %a < %x; %a++)
{
    doFunction();
}

How do I use while without crashing the server?