The difference between 'while' and 'for'
#1

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

Everything you need to know on this page.

https://sampwiki.blast.hk/wiki/Control_Structures
Reply
#3

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

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

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

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

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++;
}
Reply
#8

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)