Graphics Programs Reference
In-Depth Information
Quite often in programs, variables need to be modified in place. For
example, you might need to add an arbitrary value like 12 to a variable, and
store the result right back in that variable (for example, i = i + 12 ). This
happens commonly enough that shorthand also exists for it.
Full Expression
Shorthand
Explanation
i = i + 12
i+=12
Add some value to the variable.
i = i - 12
i-=12
Subtract some value from the variable.
i = i * 12
i*=12
Multiply some value by the variable.
i = i / 12
i/=12
Divide some value from the variable.
0x243
Comparison Operators
Variables are frequently used in the conditional statements of the previously
explained control structures. These conditional statements are based on some
sort of comparison. In C, these comparison operators use a shorthand syntax
that is fairly common across many programming languages.
Condition
Symbol
Example
<
(a < b)
Less than
>
(a > b)
Greater than
Less than or equal to
<=
(a <= b)
>=
(a >= b)
Greater than or equal to
==
(a == b)
Equal to
!=
(a != b)
Not equal to
Most of these operators are self-explanatory; however, notice that the
shorthand for equal to uses double equal signs. This is an important distinc-
tion, since the double equal sign is used to test equivalence, while the single
equal sign is used to assign a value to a variable. The statement a = 7 means
Put the value 7 in the variable a , while a == 7 means Check to see whether the variable
a is equal to 7 . (Some programming languages like Pascal actually use := for
variable assignment to eliminate visual confusion.) Also, notice that an
exclamation point generally means not . This symbol can be used by itself to
invert any expression.
! (a < b) is equivalent to (a >= b)
These comparison operators can also be chained together using short-
hand for OR and AND.
Logic
Symbol
Example
||
((a < b) || (a < c))
OR
AND
&&
((a < b) && !(a < c))
 
Search WWH ::




Custom Search