SA-MP Forums Archive
Small question... - 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: Small question... (/showthread.php?tid=389834)



Small question... - zDivine - 03.11.2012

When doing something like this:
pawn Код:
for(new i = 0; i < BLAH; i++)
Does it make a difference if you do this:
pawn Код:
for(new i = 0; i < BLAH; ++i)



Re: Small question... - Drake_Lopez - 03.11.2012

The first one is wrong, the last one is the working one..


Re: Small question... - zDivine - 03.11.2012

Quote:
Originally Posted by Drake_Lopez
Посмотреть сообщение
The first one is wrong, the last one is the working one..
Incorrect. Because I've always used 'i++' and it works fine. I just recently saw someone use ++i and didn't know if it made a difference.


Re: Small question... - TheArcher - 03.11.2012

Here's the results:

Код:
[21:46:20]  Increase of i++ 0 using <
[21:46:20]  Increase of i++ 1 using <
[21:46:20]  Increase of i++ 2 using <
[21:46:20]  Increase of i++ 3 using <
[21:46:20]  Increase of i++ 4 using <
[21:46:20]  Increase of ++i 0 using <
[21:46:20]  Increase of ++i 1 using <
[21:46:20]  Increase of ++i 2 using <
[21:46:20]  Increase of ++i 3 using <
[21:46:20]  Increase of ++i 4 using <
[21:46:20]  Increase of ++i 0 using !=
[21:46:20]  Increase of ++i 1 using !=
[21:46:20]  Increase of ++i 2 using !=
[21:46:20]  Increase of ++i 3 using !=
[21:46:20]  Increase of ++i 4 using !=
[21:46:20]  Increase of i++ 0 using !=
[21:46:20]  Increase of i++ 1 using !=
[21:46:20]  Increase of i++ 2 using !=
[21:46:20]  Increase of i++ 3 using !=
[21:46:20]  Increase of i++ 4 using !=
using

pawn Код:
main()
{
    for(new i = 0; i < 5; i++) printf(" Increase of i++ %d using <", i);
    for(new i = 0; i < 5; ++i) printf(" Increase of ++i %d using <", i);
    for(new i = 0; i != 5; ++i) printf(" Increase of ++i %d using !=", i);
    for(new i = 0; i != 5; i++) printf(" Increase of i++ %d using !=", i);
    for(new i = 0; i > 5; i++) printf(" Increase of i++ %d using  >", i);
    for(new i = 0; i > 5; ++i) printf(" Increase of ++i %d using  >", i);
}
I saw a topic where ****** said that makes no difference in PAWN but he does
Код:
for(new i = 0; i != 5; ++i)
because in C++ is faster.

Edit: That was an example, I know that I've assigned the wrong variable its acctualy int in C


Re: Small question... - Vince - 03.11.2012

Either version works in for loops. There's no difference whatsoever. The difference becomes clear when you start using this in other structures. For example:
pawn Код:
new i = 1;
printf("%d", someArray[++i]);
Will access index 2. i is incremented first.

pawn Код:
new i = 1;
printf("%d", someArray[i++]);
Will access index 1. i is incremented afterwards.