The if statement in ``block1.c'' looks like this:
if (time <= 0) {
...
}
else {
...
...
}
On the first line, the part of the statement within parentheses is the
condition that determines which part of the if will be executed. In our
example above, the condition is ``t <= 0''. When that condition is
true, the computer executes the then part of the if. But when
that condition is false, the computer executes the else part of the the
if. Thus, a condition is an expression that is either true or
false.
C provides six relational operators for comparing numbers. You will use them
to create conditions to control if statements:
- == means equal.
- != means not equal.
- > means greater than.
- < means less than.
- >= means greater than or equal.
- <= means less than or equal.
Most of the C relational operators should be pretty easy to remember, since
they look like the standard mathematical notation that you're already used to.
The tricky ones will be ``=='' for equal and ``!='' for
not equal.
For practice, let's look at some sample C conditional expressions. Translate
each of the following C expressions into an English question.
- new_position == old_position
- sin(alpha) >= 0.0
- (a * a) + (b * b) < (c * c)
Click here for the answer.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah