SA-MP Forums Archive
The difference between 'while' and 'for' - 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: The difference between 'while' and 'for' (/showthread.php?tid=334302)



The difference between 'while' and 'for' - 2KY - 14.04.2012

What is the difference, and could you perhaps give me some examples of usage?

I understand it like this.. If you could tell me if it's correct, that'd be nice.

pawn Код:
new
        myInteger = 17;
       
    while ( myInteger < 100 )
        myInteger ++;
So basically, it's like an if statement in a loop?


Re: The difference between 'while' and 'for' - iggy1 - 14.04.2012

Everything you need to know on this page.

https://sampwiki.blast.hk/wiki/Control_Structures


Re: The difference between 'while' and 'for' - 2KY - 14.04.2012

Quote:
Originally Posted by iggy1
Посмотреть сообщение
Everything you need to know on this page.

https://sampwiki.blast.hk/wiki/Control_Structures
I've read that page multiple times. The reason I make topics for these simple questions is, you never know what someone else knows, and could give you a better explanation than a static explanation on a webpage that only a select few have permission to edit.


Re: The difference between 'while' and 'for' - iggy1 - 14.04.2012

But the wiki has been reviewed by many people, i just think the info there is most likely better than what people can tell you here. Plus that page gives a complete explanation of both statements.

I think in the rules of this forum it says you must search before posting threads. (or at least it used to)


Re: The difference between 'while' and 'for' - Shetch - 14.04.2012

Just like the wiki says.
A 'for' loop is the same as the 'while' loop, only compressed. That means you don't need to write the 'new a;' and 'a++;'. Hope you get what i mean.


Re: The difference between 'while' and 'for' - Vince - 14.04.2012

For is used when you know how many times you want to iterate, while is for the other case. For example; you don't usually know how many lines there are in a file or how many rows were returned by SQL query.


Re: The difference between 'while' and 'for' - Shetch - 14.04.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
For is used when you know how many times you want to iterate, while is for the other case. For example; you don't usually know how many lines there are in a file or how many rows were returned by SQL query.
Yes, but couldnt you just do it like
Код:
new
	a = 0;
while (a < 'MAX_PLAYERS, FILE_LINES or ROWS')
{
	// Code in the loop
	a++;
}



Re: The difference between 'while' and 'for' - iggy1 - 14.04.2012

As long as the expression after 'while' evaluates to true the code will loop.


AW: The difference between 'while' and 'for' - Nero_3D - 14.04.2012

if you check the assembly you will notice that

for uses three jumps
pawn Код:
//
    new i = 0;
    goto loop;

    increase:
    i += 1;

    loop:
    if(/*inverted statement*/) {
        goto end;
    }
    //code
    goto increase;

    end:
while two jumps
pawn Код:
//
    new i = 0;

    loop:
    if(/*inverted statement*/) {
        goto end;
    }
    //code
    goto loop;

    end:
and do while three jumps
pawn Код:
//
    new i = 0;

    loop:
    //code

    continue:
    if(/*inverted statement*/) {
        goto end;
    }
    goto loop;

    end: