Raised This Month: $ Target: $400
 0% 

increase understanding.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-27-2007 , 17:04   increase understanding.
Reply With Quote #1

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?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
djmd378
Senior Member
Join Date: Sep 2004
Old 08-27-2007 , 19:27   Re: increase understanding.
Reply With Quote #2

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
    
}

__________________
djmd378 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-27-2007 , 19:49   Re: increase understanding.
Reply With Quote #3

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 =]
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-27-2007 , 20:42   Re: increase understanding.
Reply With Quote #4

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.

Last edited by Lee; 08-27-2007 at 20:51.
Lee is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-27-2007 , 20:50   Re: increase understanding.
Reply With Quote #5

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
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 08-28-2007 , 07:38   Re: increase understanding.
Reply With Quote #6

Yep,

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

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

If that doesn't just confuse you further
purple_pixie is offline
potatis_invalido
BANNED
Join Date: Jul 2007
Location: 0 (world)
Old 08-28-2007 , 09:21   Re: increase understanding.
Reply With Quote #7

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.

Last edited by potatis_invalido; 08-28-2007 at 09:24.
potatis_invalido is offline
Reply


Thread Tools
Display Modes

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 16:11.


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