This will go over the basic mathematical, comparison, and boolean operators.
They are essential for use in conditional statements, and for doing math. surprise
The arithmetic operators are:
+ ...plus, add
- ...minus, subtract
/ ...divided by, division
* ...multiplied by, multiplication
and the less famous
% ...modulus operator (see the secret modulus page for info on this mystery operator.)
The comparison operators are:
< less than
> greater than
<= less than or equal too
>= greater than or equal too
!= not equal to
and
== equal too. Do Not confuse == with =.
= is the assignment operator. It assigns a value to a variable.
== compares two values.
for example:
using some basic conditional statements:
{
int $number = 12;
print ("The number is " + $number + "\n");
if ($number == 13)
print "This number is thirteen.\n";
print ("The number is " + $number + "\n");
}
that won't print anything, because 12 is not 13. But change it to:
{
int $number = 12;
print ("The number is " + $number + "\n");
if ($number = 13)
print "This number is thirteen.\n";
print ("The number is " + $number + "\n");
}
What happens in the second example is this:
($number = 13) assigns the value 13 to $number. It is a successfull operation,
so the result is a boolean yes, or 1. 1 is always true, so the conditional evaluates,
and the number is changed to 13
This sort of mistake can lead to really frustating bugs.
A good way to avoid this happening is to phrase the == operator this way:
if (13 == $number)
then, if you leave off one of the =, you'll get an error message from that line
and know exactly where to fix it.
Boolean Operators
&& the and operator
|| the or operator (the | is usually found on the button above the enter key)
and
! the not operator.
The && and the || operators are used to combine conditions together in an expression:
The && operator requires the expressions on either side to evaluate true, in order for it
to pass.
{
int $one = 1;
int $two = 2;
if ($one == 1 && $two == 2)
print "I Win.\n";
}
The || operator only requires one expression to be true
{
int $one = 1;
int $two = 3;
if ($one == 1 || $two == 2)
print "I Win.\n";
}
the ! operator is used on the != not-equal to operator, and to reverse the
result of an expression.
{
int $one = 13;
int $two = 54;
if ($one != 1 && $two != 2)
print "I lose.\n";
}
a condition with 1 for the expression will always go through, because 1 == true.
{
if (1)
print "True";
}
a condition with 0 for the expression will never go through, becuase 0 == false
{
if (0)
print "False";
}
Nothing happened. The ! operator reverses the boolean value of an expression so:
{
if (!0)
print "True";
}
Now it prints.
Increment and Decrement operators
++ add 1 to current value
-- subtract 1 from current value
{
int $num = 10;
$num++;
print ($num + "\n");
$num--;
print ($num + "\n");
}
These other operators are a little different, but the idea is the same.
+=
-=
*=
/=
%=
they need a value on the other side.
$variable *= 12; is the same as $variable = $variable * 12;
The rest work the same way, including the %= operator modulus again!!!
These operators can only be explained so much.
The best way to learn them is through experimenting with them.
|