Difference between 'Loop' & 'While'
#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


Messages In This Thread
Difference between 'Loop' & 'While' - by [D]ry[D]esert - 08.08.2013, 13:43
Re: Difference between 'Loop' & 'While' - by Black Wolf - 08.08.2013, 13:50
Re: Difference between 'Loop' & 'While' - by [D]ry[D]esert - 08.08.2013, 13:56
Re: Difference between 'Loop' & 'While' - by Vince - 08.08.2013, 13:56
Re: Difference between 'Loop' & 'While' - by [D]ry[D]esert - 08.08.2013, 14:01
Re: Difference between 'Loop' & 'While' - by [D]ry[D]esert - 09.08.2013, 02:18

Forum Jump:


Users browsing this thread: 1 Guest(s)