AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Pawn Tutorial on wiki (https://forums.alliedmods.net/showthread.php?t=120124)

Doc-Holiday 03-01-2010 01:13

Pawn Tutorial on wiki
 
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++

ConnorMcLeod 03-01-2010 01:22

Re: Pawn Tutorial on wiki
 
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)

Doc-Holiday 03-01-2010 01:30

Re: Pawn Tutorial on wiki
 
Thanks for the clarifications on to page two lol.

joropito 03-01-2010 07:22

Re: Pawn Tutorial on wiki
 
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"

Seta00 03-01-2010 16:48

Re: Pawn Tutorial on wiki
 
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

Exolent[jNr] 03-01-2010 17:39

Re: Pawn Tutorial on wiki
 
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.

Seta00 03-01-2010 22:09

Re: Pawn Tutorial on wiki
 
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.

Exolent[jNr] 03-01-2010 22:12

Re: Pawn Tutorial on wiki
 
Quote:

Originally Posted by Seta00 (Post 1104999)
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.

Seta00 03-01-2010 22:19

Re: Pawn Tutorial on wiki
 
I'm not arguing with you, I was just explaining that this:
Quote:

Originally Posted by Seta00 (Post 1104730)
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.

Doc-Holiday 03-02-2010 00:51

Re: Pawn Tutorial on wiki
 
Quote:

Originally Posted by Seta00 (Post 1105009)
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.


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

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