ATSC 212 - C
G$+0.;"09)
23!
Now that we have variables, it is time to do something with them.
Operators are like built in functions that will perform an action on
variables. There are two types of operators in C, mathematical
and logical. All operators can be used on integers, floats, and
characters, and to some extent, pointers.
Mathematical operators include =, -, --, +, ++, *, /, %, -=, +=,
*=, and /=. The % operator is the modulus. -=, +=, *= and /=
apply the mathematical operator to the value on the left with
whatever is on the right (ie a *= b; is the same as a = a * b;).
Care must be taken when using anything but = with characters
and pointers. Results are system dependant. These operators
work with regular variables or array elements. The ++/--
operators increment/decrement a variable by 1.
Logical operators include <, <=, ==, !=, >=, >. There are no
logical operators for strings, however, there are functions for
comparing strings as we will see.
ATSC 212 - C
!"'8%6"'.&9)
24!
It is all about choice. Just as with other languages, sometimes we
would like to choose what actions we take. C has one conditional
– the if statement:
if (<condition>) { …code block… }
else if (<condition>) { …code block… }
else { …code block… }
The else if and else portions are optional. C is different from some
other languages in that it does not have a true boolean type. Any
condition that evaluates to 0 is false, all others are true.
The logical and mathematical operators can be used to form
conditions. This includes assignment (=) and equality (==). A
big pitfall of programmers is accidentally using assignment where
they mean to compare two things. The condition a == b checks if
a and b are the same. The condition a = b sets the value of a to
the value of b and returns true.