-
JavaScript Conditionals
Okay, I've just finished a big thick book on JavaScript, but unfortunately something wasn't explained to me. I know lost of conditionals, like ==, &&, +=, <=, !=, >=, <, and >, but when I look at advanced scripts I see ++...can someone explain this one to me? I'd also appreciate some sort of list of conditionals, since there are probably others.
Greatly Appreciated!
-
It's not a conditional test.
It's an arithmatic function called autoincrement ( and hencely autodeincrement )
$i=5;
$i++; // return $i then increment it
++$i; // increment $i then return it
thusly if you did:
$i = 20;
$d = $i++; // $i = 21 and $d = 20
$i = 20;
$d = ++$i; // $i = 21 and $d = 21
The same is true of --
if you want to remember which returns which think of it from left to right. $d=$i++; < thus $d = $i THEN you increment it. $d=++$i < thus $d = Increment THEN $i
okie
Hope this helps
Flawless
-
After thought:
you might have seen this in a for loop
for ($i=0;$i<10;$i++)
ie:
for (starting value; condition to end upon; upon each iteration)
hencely on the first iteration $i = 0 - since it retruns THEN increments.
This is used less often now as people prefer to use
for (number .. end )
as a sequence system - but this is because they are normally
just caring about the number of times their iteration gets run - rather than the number of the current iteration.
Flawless
( btw - excuse the $i's - i'm working my *** of in perl modules right now )
-
As well as the ++ (increment) and -- (decrement) operators, there are also may operators for manipulating a variable's value relative to its present variable.
e.g. "tmp *= 2" is the same as "tmp = tmp * 2"
Any good reference on JS or C++ should list and explain all of these operators, such as Microsoft's JScript reference (see my sig).
M@rco