Usage of loops
#1

Been through the SA-MP wiki and some other websites and tutorials, learned alot but I still can't understand or get any of the kinds of loops whether it is the for loop for while loop or do loop. I don't understand the usage (for what they are used and what can I create with loops) and the way to create a loop (creating loops in a script).
Can someone explain what loops do (for a complete beginner) and how to carry out loops and what are their usages. If you provide what I can do with it in an example I will understand better. Anyhow, I really need help with this. Thanks in advance.
Reply
#2

Check out the PAWN Language Guide, that might help you out a bit more!

http://www.compuphase.com/pawn/Pawn_Language_Guide.pdf

This might help too:
http://www.compuphase.com/pawn/Pawn_...nter_Guide.pdf
Reply
#3

Examples:
Set a value for all players.
pawn Код:
for (new i=0; i < MAX_PLAYERS; i++)
{
     if (IsPlayerConnected(i))
     {
           Value[i] = 1;
     }
}
[pawn]
for ( // create the loop
new i = 0; //create a new zero variable
i < MAX_PLAYERS; // same with if (i < MAX_PLAYERS)
i++ ) // code to execute after every run (Increments i)



2nd example:
(same)
pawn Код:
new i = 0;
while (i < MAX_PLAYERS)
{
     if (IsPlayerConnected(i))
     {
           Value[i] = 1;
     }
     i++;
}
3rd example:
(same)
pawn Код:
new i = 0;
until (i == MAX_PLAYERS)
{
     if (IsPlayerConnected(i))
     {
           Value[i] = 1;
     }
     i++;
}
Reply
#4

lets say you want to loop the text:
"The red brown fox jumps over the lazy dog",

you want to know at what position THE FIRST 'a' is. so let's say it's stored in:
pawn Код:
new string[64];
you loop the whole string till you find an 'a' so:
pawn Код:
// syntax for 'for' loop:
// for ( <executed code once, for example declare an variable, more code can be done with comma's ex: new a,b,c,a=b=c=0>; <statement which is true, if false loop exits>; <code executed every loop call, for example: a= a+1>)
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{


}
that will make the loop, now we need to execute something in it each call:
pawn Код:
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{
    if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
    {
       
    }
}
ok now we check the availability for the character 'a'.

but what happen if we return something in the loop?:

pawn Код:
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{
    if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
    {
       
    }
    return 1;// this will preven the whole loop, whatever you return, the loop will be executed only once or not at all (because the statement matters too)
}
however:
pawn Код:
stock function()
{
    for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
    {
        if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
        {
            return counter_variable; // loop will end, everything below will not be executed, so -1 won't be returned, but the position of the first 'a' will get returned 
        }
    }
    return -1//not found
}
just look out your loop will not be unlimited:
pawn Код:
for(new variable = 1; variable > 0; ++variable)
{
    //will teoretically never end, however if 2^31 is reached, it will be ended because of an integer overflow
}
while is the same but is is just
pawn Код:
while(statement)//if statement is false (0) while will stop looping
{//do
    //this
}
like:
pawn Код:
stock function()
{
    new counter_variable = 64;//set to 64, however now it will return the position of the LAST 'a'
    while(--counter_variable)
    {
        if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
        {
            return counter_variable; // loop will end, everything below will not be executed, so -1 won't be returned, but the position of the first 'a' will get returned 
        }
    }
    return -1//not found
}
and do.. well if u see any code used , then just look back a this and i will understand it.. pure 100% logic.
Reply
#5

Quote:
Originally Posted by hittt
Посмотреть сообщение
Examples:
pawn Код:
new i = 0;
until (i == MAX_PLAYERS)
{
     if (IsPlayerConnected(i))
     {
           Value[i] = 1;
     }
     i++;
}
Never heard of the until one, that looks easy. Though I will work on it, now I am going to die for some hours. I mean sleep.
Reply
#6

Quote:
Originally Posted by suhrab_mujeeb
Посмотреть сообщение
Never heard of the until one, that looks easy. Though I will work on it, now I am going to die for some hours. I mean sleep.
And i just found out that until doesnt really exist in pawn.

You can easily make it with this thought.

pawn Код:
#define until(%1) while(!(%1))
Reply
#7

@Gamer_Z, thanks for the help.

Can anyone show me an example of what I can do with it, yeah hittt gave an example which I understood very well but another more used example that will help me understand better.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)