SA-MP Forums Archive
[Help] Control structures - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [Help] Control structures (/showthread.php?tid=68584)



[Help] Control structures - Coicatak - 11.03.2009

Well I don't really understand how "static" remembers its old statment, why does it print "1 2 3" whereas j is set to 1 at each loop? (look at the last example) It should always be 1, shouldn't it?
Quote:
Originally Posted by SAMP Wiki
static local

A static local can be used in the same place as a local but doesn't forget it's old value, for example:

pawn Код:
MyFunc()
{
  static
    var1 = 4;
  printf("%d", var1);
  {
    // var1 still exists as this is a lower level
    static
      var2 = 8;
    printf("%d %d", var1, var2);
  }
  // var2 no longer exists as this is a higher level
}
// var1 no longer exists
That code will behave exactly the same as the new example, however this:

pawn Код:
for (new i = 0; i < 3; i++)
{
  static
    j = 1;
  printf("%d", j);
  j++;
}
Код:
Will print:

1
2
3
Because j is static so remembers it's old value.
__________________________________________________ ________________________________


Quote:
Originally Posted by SAMP Wiki
break

break breaks out of a loop, ending it prematurely:

for (new a = 0; a < 10; a++)
{
if (a == 5) break;
}

This loop will go 6 times but code after the break will only be executed 5 times.
So if I do:
pawn Код:
for (new i=0; i<MAX_PLAYERS;i++
{
    if(IsPlayerConnected(i))
    {
       if(i == 45) break;
    }
}
The loop will stop as soon as it will reach the 45th player (or 46th?) right?
And then if I put return 1; somewhere, will it have the same effect as break or not?

__________________________________________________ _____________________
Thanks in advance.


Re: [Help] What's the difference between new and static - MenaceX^ - 11.03.2009

There is a whole huge guide about pawn language, I don't remember it's name but search for it, It helpsfull.


Re: [Help] What's the difference between new and static - Coicatak - 11.03.2009

Quote:
Originally Posted by MenaceX^
There is a whole huge guide about pawn language, I don't remember it's name but search for it, It helpsfull.
You mean PAWN language guide on PAWNO site? Well I have searched but found nothing about what I want to know


Re: [Help] Control structures - Marcel - 11.03.2009

pawn-lang.pdf


Re: [Help] Control structures - Coicatak - 12.03.2009

Quote:
Originally Posted by Marcel
-->

Quote:
Originally Posted by Coicatak
Quote:
Originally Posted by MenaceX^
There is a whole huge guide about pawn language, I don't remember it's name but search for it, It helpsfull.
You mean PAWN language guide on PAWNO site? Well I have searched but found nothing about what I want to know
--'


Re: [Help] Control structures - Google63 - 12.03.2009

First is because it has 'j++' that is same as 'j += 1' so every time it loop add 1 to variable 'j' - if that was your question
and second - return and break are NOT SAME as you can see.. BREAK - break/stop loop > and return - return what you want to...

Correct Me If I Am Wrong


Re: [Help] Control structures - Finn - 12.03.2009

This loop will stop when i is 45, so it means it stops at player id 45, which is 46th player, because 45 ids + id 0 = 46.

pawn Код:
for (new i=0; i<MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        if(i == 45) break;
    }
}

Break stops just the loop, return stops the whole function. In this example "YAY" will not be printed, but if you used break instead of return, "YAY" would be printed.

pawn Код:
main()
{
    for(new i; i < 20; i++)
    {
        printf("i = %d", i);
        if(i == 10) return 1;
    }
    print("YAY");
    return 1;
}



Re: [Help] Control structures - Coicatak - 12.03.2009

Quote:
Originally Posted by Finn
This loop will stop when i is 45, so it means it stops at player id 45, which is 46th player, because 45 ids + id 0 = 46.

pawn Код:
for (new i=0; i<MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        if(i == 45) break;
    }
}

Break stops just the loop, return stops the whole function. In this example "YAY" will not be printed, but if you used break instead of return, "YAY" would be printed.

pawn Код:
main()
{
    for(new i; i < 20; i++)
    {
        printf("i = %d", i);
        if(i == 10) return 1;
    }
    print("YAY");
    return 1;
}
Ok, I understood, thanks dude.

Quote:
Originally Posted by ******63
First is because it has 'j++' that is same as 'j += 1' so every time it loop add 1 to variable 'j' - if that was your question
You're actually wrong, cause the j++ appears after the printf. But at each loop j is set to 1 and then increased ("j<3; j++") to 1 so j would be 1 then 2 then 1 then 2 then 1 for an infinite loop! (assuming new has the same effect than static)