Reference
if Statement
©
Carnegie Mellon Robotics Academy / For use with VEX
®
Robotics Systems
if Statements with Natural Language
if(condition)
{
//true-commands
}
Pseudocode of an if Statment:
An if Statement allows your robot to make a decision. When your robot reaches an if Statment in the
program, it evaluates the condition contained between the parenthesis. If the condition is true, any
commands between the braces are run. If the condition is false, those same commands are ignored.
(true) commands
Commands placed here will run
if the (condition) is true.
(condition)
Either true or false
Example program containing two if Statements:
taskmain()
{
while(true)
{
if(SensorValue(bumper)==0)
{
startMotor(port3,63);
}
if(SensorValue(bumper)==1)
{
stopMotor(port3);
}
}
}
This program uses a Bumper Switch and two if Statements to control when the port3 motor moves.
The rst if Statement sets the motor to half power forward if the Bumper Switch has not been
pressed, while the second turns the motor off if it has been pressed. Continually repeating these
two behaviors within the while loop causes the motor to spin forward while the Bumper Switch is
released, and to remain stopped for as long as it is pressed.
(true) commands
Commands here run if the
(condition) is true.
(condition)
true if the sensor is unpressed; false otherwise
(true) commands
Commands here run if the
(condition) is true.
(condition)
true if the sensor is pressed; false otherwise