Raised This Month: $ Target: $400
 0% 

Pawn Tutorial on wiki


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 03-01-2010 , 01:13   Pawn Tutorial on wiki
Reply With Quote #1

Question for you guys real quick to make sure i understand this seems simple enough
PHP Code:
//Example of a for loop
new i
new sum
for (i=1i<=10i++)
{
   
sum += i

Steps this follows
</span>1. Check if the condition is true. If so, continue. If not, stop.
2. Run the code.
3. Run the "increment" parameter.
4. Go to step 1.


Now if i do it like.
PHP Code:
//Example of a for loop
new i
new sum
for (i=1i<=10; ++i)
{
   
sum += i

Steps this follows
1. Check if the condition is true. If so, continue. If not, stop.
2. Run the "increment" parameter.
3. Run the code.
4. Go to step 1.



Would that be the difference between the ++i and i++
Doc-Holiday is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 03-01-2010 , 01:22   Re: Pawn Tutorial on wiki
Reply With Quote #2

There is no differences, the increment paramater is always done after the code has been executed <- i don't know for that part, but the condition is checked before for sure.

++i or i++ makes no difference in a for loop.

In the following code, difference would be that in function 1 checks starts with i == 2 and in fonction2 checks starts with 1.
So, ++i, i is incremented first and its result is then returned, with i++, i value is returned before it is incremented.
PHP Code:
function1()
{
    new 
1
    
while( ++<= 10 )
    {
        
// 
    
}
}

function2()
{
    new 
1
    
while( i++ <= 10 )
    {
        
// 
    
}


Would have an importance in a for loop if you would do something like :

for(new i=val; ++i < SOME_VALUE; some other check)
__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 03-01-2010 at 01:24.
ConnorMcLeod is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 03-01-2010 , 01:30   Re: Pawn Tutorial on wiki
Reply With Quote #3

Thanks for the clarifications on to page two lol.
Doc-Holiday is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 03-01-2010 , 07:22   Re: Pawn Tutorial on wiki
Reply With Quote #4

Here's another example

PHP Code:
new 1
new = ++i    // Increase I and then assign value to X
server_print("%i %i"ix
This will output "2 2"

But with this other code

PHP Code:
new 1
new i++    // assign value to X and then increase I
server_print("%i %i"ix
Will output "2 1"
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 03-01-2010 , 16:48   Re: Pawn Tutorial on wiki
Reply With Quote #5

Prefix operator does his job before the expression evaluation.
Postfix operator evaluates to the original value, and then increments the variable.

EDIT: Postfix = c++, prefix = ++c
Seta00 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-01-2010 , 17:39   Re: Pawn Tutorial on wiki
Reply With Quote #6

Prefix and postfix have no difference in this situation.

PHP Code:
for( initialconditionincrement )
{
    
code
}
end of loop 
1. initial
2. if condition false, exit loop without doing anything else (no increment or code executed)
3. execute code
4. increment
5. repeat back to #2

It's that simple.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 03-01-2010 , 22:09   Re: Pawn Tutorial on wiki
Reply With Quote #7

I know. My explanation wasn't on this specific case, since the last for instruction is executed on it's own.
And the third for parameter doesn't need to be an increment, it's just a statement.
Seta00 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-01-2010 , 22:12   Re: Pawn Tutorial on wiki
Reply With Quote #8

Quote:
Originally Posted by Seta00 View Post
And the third for parameter doesn't need to be an increment, it's just a statement.
That is what that piece of the loop is described as.
I wasn't naming it like that just because it's supposed to be an increment.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 03-01-2010 , 22:19   Re: Pawn Tutorial on wiki
Reply With Quote #9

I'm not arguing with you, I was just explaining that this:
Quote:
Originally Posted by Seta00 View Post
Prefix operator does his job before the expression evaluation.
Postfix operator evaluates to the original value, and then increments the variable.

EDIT: Postfix = c++, prefix = ++c
Is a general rule, and should be remember when messing up with macros and arithmetic.
I'm just helping others, not correcting you. We're all building content for newcomers.
Seta00 is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 03-02-2010 , 00:51   Re: Pawn Tutorial on wiki
Reply With Quote #10

Quote:
Originally Posted by Seta00 View Post
I'm not arguing with you, I was just explaining that this:

Is a general rule, and should be remember when messing up with macros and arithmetic.
I'm just helping others, not correcting you. We're all building content for newcomers.
I understood that part just was wondering what the importance of it was but when i think it was conor said that

in a while loop ++i and i++ are pretty important depending on what you are doing.
Doc-Holiday is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode