Difference between 'Loop' & 'While'
#1

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.
Reply
#2

First of all While is a loop.
Reply
#3

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 ?
Reply
#4

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.
Reply
#5

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.
Reply
#6

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)