ROBOTC
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:
taskmain()
{
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
ROBOTC
Reference
if Statement
©
Carnegie Mellon Robotics Academy / For use with VEX
®
Robotics Systems
if-else Statements with Natural Language
if(condition)
{
//true-commands
}
else
{
//false-commands
}
Pseudocode of an if-else Statment:
taskmain()
{
while(true)
{
if(SensorValue(sonarSensor)>25)
 {
 startMotor(port3,63);
 }
 else
 {
stopMotor(port3);
}
}
}
The if-else Statement is an expansion of the basic if Statement. The “if” section still checks the
condition and runs the appropriate commands when it evaluates to true, but using the “else” allows
for specic code to be run only when the condition is false.
Example program containing an if-else Statement:
This if-else Statement tells the robot to run port3 at half power if the nearest object the Ultrasonic
Rangender detects is more than 25 centimeters away. If the Ultrasonic Rangender detects an
object closer than 25 centimeters, then the “else” portion of the code will be run and the motor on
port3 will stop moving. The outer while(true) loop makes the if-else statement repeat forever.
(true) commands
Commands placed here will run
if the (condition) is true.
(false) commands
Commands placed here will run
if the (condition) is false.
(condition)
Either true or false.
(true) commands
Commands here run if the
(condition) is true.
(false) commands
Commands here run if the
(condition) is false.
(condition)
true if the sensor reads over 25;
false otherwise
ROBOTC
Reference
Embedded if/if-else Statements
with Natural Language
if(condition)
{
if(condition)
{
 //true-commands
}
else
{
//false-commands
}
}
Pseudocode of an embedded if Statment:
Sometimes, especially with more complex tasks, your robot will have to make multiple consecutive
decisions before performing a behavior. This can be accomplished by embedding, or placing, if
Statments within other if Statements.
(condition)
Either true or false
if Statement
©
Carnegie Mellon Robotics Academy / For use with VEX
®
Robotics Systems
(true) commands
Commands placed here will run
if the (condition) is true.
(false) commands
Commands placed here will run
if the (condition) is false.