| CSC 161 | Grinnell College | Spring, 2012 |
| Imperative Problem Solving and Data Structures | ||
Conditional if statements operate on Boolean values. An expression such as:
1 == 2;
returns true if the statement is correct, and false otherwise. The above expression evaluates to false. This is the standard behavior of all the comparison and logical operators in C: ==, <=, >=, <, >, !=, &&, and, ||.
It is easy to misuse the comparison operators. In mathematics, the expression:
-2 < -1 < 0
makes sense and would evaluate to true. However, in C,
the semantics of these expressions is different, and the above
expression in C actually evaluates to false.
Specifically, comparison operators associate left to right and
evaluate to Boolean values, so
-2 < -1 < 0 is evaluated as if
parentheses were added to give
(-2 < -1) < 0. The value
returned from the first comparison is 1 (true) because
-2 is less than -1. Next, 1 is compared to 0. 1 is not less than
0 so the expression evaluates to false.
To do multiple tests at once, use logical AND (&&), and logical OR (||) operators. For example, the previous example can be correctly expressed in C as:
(-1 > -2) && (-1 < 0)
This expression in C works fine, but note that some
simplification is possible, because the &&
operation has lower precedence than either >
or <. Taking advantage of the assumed precedence
of these options, the parentheses can be removed from the above
expression, and C will still understand that it should perform
the comparisons with > and <
operations before applying
&&. Thus, many C programmers will simplify
the above expression to
-1 > -2 && -1 < 0
In C, anything that evaluates to a 0 will mean false and oppositely anything that is non-zero will evaluate to be true. We can use this property of Booleans in C to simplify conditionals. Here is how:
if( rGetIRTxt("left") = 1)
rTurnLeft(1,1);
else
rForward(1,1);
The code above says if the call for the left sensor value is 1, then turn left, if not just go forward. But since 1 is a non-zero value, it will be true, so we don't have to write it. We could just say:
if( rGetIRTxt("left") )
rTurnLeft(1,1),
else
rForward(1,1);
Similarly, if we wanted the robot to beep whenever there was nothing in front of it, and just go forward if there is nothing to beep for, we could use the same property. The only change we would need to do would be to negate so that it would say "if rGetIRTxt() returns a not true value, beep". Here is the code:
if( ! rGetIRTxt("left") )
rForward(1,1),
else
rBeep(1,500);
Conditional statements (ifs) are designed to work with Boolean values. If an expression is true, the specified code will execute. In addition to all the logical and comparison operators in C, many functions also return Boolean values to indicate different outcomes.
It is a good idea to avoid testing against Boolean values in your conditionals if a value is already considered to be Boolean. If a function or expression is supposed to return true on success and false on failure, do not further test that result against true or false. Consider the following:
if ( true )
and:
if ( true == true )
Initially, it may seem the second case is more accurate than the first. But, add a third case:
if ( true == true == true )
The third case is no more descriptive or accurate than the second in the same way the second is no more accurate than the first. It is good practice to avoid this kind of redundancy in your testing.
Since comparison and logical operators in C evaluate to Boolean values, good coding practice takes advantage of this evaluation when returning Boolean results from functions. For example, consider the following code segment:
if ( x == y )
return 1;
else
return 0;
This code may be correct, but it also is inefficient and wordy.
Think about how operators which return Boolean values (such as
the == operator) work. If x
and y have common values, then the result of
the comparison x == y is true — which in C is
represented by the value 1. If the comparison is false, the
result is false, represented by the value 0. Thus, the return
values are exactly what C computes with the statement x
== , and the complex statement above can be simplified to
return ( x == y );
Instead of using many if statements, we can use switch statements to simplify our code. When there are cases in which a variable is compared to other integer values, and a certain code is executed if the compared values are equal, we can use switch cases.
Here is how to use switch statements:
switch (variable){
case value0:
//Execute this code if variable == value0
break;
case value1:
//Execute this code if variable == value1
break;
case value2:
//Execute this code if variables == values2
break;
.
..
...
default:
Execute this code if variable did not equal any of the cases above
break;
}
The program second-counter.c provides an
example of how switch statements work.
Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.