SA-MP Forums Archive
Difference between 'Loop' & 'While' - 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)
+--- Thread: Difference between 'Loop' & 'While' (/showthread.php?tid=456991)



Difference between 'Loop' & 'While' - [D]ry[D]esert - 08.08.2013

Hello there!
As the topic said, I really wanna know what the different between 'Loop' & 'While'
I've read samp wiki and still i didnt get it..
what 'while' can do and 'loop' cant ?
As i understand, while have the same function of loop, but some people told me NO
I will be much thankful if someone explained me.


Re: Difference between 'Loop' & 'While' - Black Wolf - 08.08.2013

First of all While is a loop.


Re: Difference between 'Loop' & 'While' - [D]ry[D]esert - 08.08.2013

Quote:
Originally Posted by Black Wolf
Посмотреть сообщение
First of all While is a loop.
Ah, Every While is a loop, What about 'Every loop is a while' ?\
I've notice that people use while in reading lines from file and load it..
So i'll be able to use for(....) instead using while, right ?


Re: Difference between 'Loop' & 'While' - Vince - 08.08.2013

There is no such thing as a 'loop' keyword in Pawn.

for is used when you know how many iterations you want to do; while is used when you don't know.
While loops are typically used for reading data from a resource, such as files or SQL, since these resources can return an arbitrary amount of data.


Re: Difference between 'Loop' & 'While' - [D]ry[D]esert - 08.08.2013

Quote:
Originally Posted by Vince
Посмотреть сообщение
There is no such thing as a 'loop' keyword in Pawn.

for is used when you know how many iterations you want to do; while is used when you don't know.
While loops are typically used for reading data from a resource, such as files or SQL, since these resources can return an arbitrary amount of data.
Thanks for sharing these Valuable information.
Much appreciated.


Re: Difference between 'Loop' & 'While' - [D]ry[D]esert - 09.08.2013

Quote:
Originally Posted by ******
Посмотреть сообщение
Actually, the ONLY difference between "for" and "while" is variable scope and increment time:

pawn Код:
new i = 0;
while (i != 10)
{
    continue;
    ++i;
}
// i can be used here, but the loop will run forever.
pawn Код:
for (new i = 0; i != 10; ++i)
{
    continue;
}
// i can't be used here, and the loop will end.
A perfect replication of the for loop above using while would look like:

pawn Код:
{
    // Restrict the scope.
    new i = 0;
    while (i != 10)
    {
        goto _continue;
_continue:
        ++i;
    }
}
// i can't be used here, and the loop will end.
You could also write the while loop using for:

pawn Код:
new i = 0;
for ( ; i != 10; )
{
    continue;
    ++i;
}
// i can be used here, but the loop will run forever.
And because no one else has mentioned it:

pawn Код:
do
{
}
while (cond);
Is always run at least once because the code comes before the check.
Wow, Thanks, That was very useful.
You've mentioned 'Forever loop (Not ended loop)', It's going to slow down the script, right ?
What what caused 'While' to be forever loop is 'Continue', right ?
Thanks again.