Programming instructions
Reference
Project Lead The Way
©
and Carnegie Mellon Robotics Academy
©
/ For use with VEX
®
Robotics Systems
Boolean Logic • 3
Boolean Logic
Logical Operators
Some (conditions) need to take more than one thing into account. Maybe you only want the robot
to run if the trafc light is green AND there’s no truck stopped in front of it waiting to turn. Unlike
the comparison operators, which produce a truth value by comparing other types of values (is one
number equal to another?), the logical operators are used to combine multiple truth values into one
single truth value. The combined result can then be used as the (condition).
Example:
Suppose the value of a Light Sensor named sonarSensor is 50, and at the same time, the value of
a Bumper Switch named bumper is 1 (pressed).
The Boolean statement (sonarSensor > 45) && (bumper == 1) would be evaluated...v
ROBOTC
Symbol
Meaning Sample comparison Result
&& “AND”
true && true
true
true && false
false
false && true
false
false && false
false
|| “OR”
true || true
true
true || false
true
false || true
true
false || false
false
(50 > 45) && (1 == 1)
true && true
true
Use in Control Structures
“Under the hood” of all the major decision-making control structures is a simple check for the
Boolean value of the (condition). The line if (SensorValue(bumper) == 1)... may read
easily as “if the bumper switch is pressed, do...”, but the robot is really looking for if(true) or
if(false). Whether the robot runs the “if true” part of the if-else structure or
the “else” part, depends solely on whether the (condition) boils down to true or false.
if (50 > 45) ...
if (true) ...
Go to Reference Links