AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   increase understanding. (https://forums.alliedmods.net/showthread.php?t=60139)

Exolent[jNr] 08-27-2007 17:04

increase understanding.
 
as you know, you can do
Code:
variable++
to increase the variable by 1.

i understand this:
Code:
new variable; public repeating_task() {     variable++;     if(variable > 2) variable = 0;     // do something with variable. }

and i know how it works in the "for" loops.

what i dont understand is the way im using it here:
Code:
new variable; public repeating_task() {     if(variable > 3) variable = 0;     switch(variable++)     {         case 0: // something...         case 1: // something...         case 2: // something...         case 3: // something...     } }

now in the switch, say the variable was greater than three and it was reset to 0.
does the switch use the increased variable or does the variable increase after the switch?

djmd378 08-27-2007 19:27

Re: increase understanding.
 
PHP Code:

public repeating_task()
{
    
// Lets say Variable is currently at 3
    
if(variable 3variable 0;
    switch(
variable++)// It'll come here and then it'll be 3+1 so then it'll be 4
    
{
        case 
0// something... skips
        
case 1// something... skips
        
case 2// something... skips
        
case 3// something... skips
        
case 4// It is 4 because it won't be set back to 0 because it's being added right after the check to see if its greater than 3
    
}



Exolent[jNr] 08-27-2007 19:49

Re: increase understanding.
 
well i believe it doesnt increase the variable until after the switch because i have debug msgs and it showed that case 0 would show up, which means it wouldnt increase the 0 to 1. (i didnt debug till after i posted. sryz)

so unless someone else seconds your statement, im all set =]

Lee 08-27-2007 20:42

Re: increase understanding.
 
As you have found, djmd378 is mistaken.

var++ - post-increment - returns var, then increments var
++var - pre-increment - increments var, then returns var

The difference between the two doesn't make a practical difference if you use them as a single statement - only if you use the return value.

Exolent[jNr] 08-27-2007 20:50

Re: increase understanding.
 
i thought i was right, but i didnt know what you just showed me O.o
i thought that variable++ and ++variable were the same but i guess not.

tnx lee =D

purple_pixie 08-28-2007 07:38

Re: increase understanding.
 
Yep,

i == i ++
but i++ != i

(And ++i == i but i != ++i )

If that doesn't just confuse you further

potatis_invalido 08-28-2007 09:21

Re: increase understanding.
 
i = 1;

i == i ++
first it checks if i == i, wich of corse is true, then it sets i to i+1.

i++ != i
first it stores the value of i, 1, then it sets i to i+1, and then it checks if 1 == i where i is 2.

++i == i:
first i is set to i+1, then it checks if i == i, wich of corse is true.

i != ++i becose it first stores 1, then it adds 1 to to i, and then it will check if 1 == 2, which of corse is false.




I hope you understand. Everything works as it should work.


All times are GMT -4. The time now is 16:12.

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