Programming instructions

Reference
Project Lead The Way
©
and Carnegie Mellon Robotics Academy
©
/ For use with VEX
®
Robotics Systems
If Statements
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
Go to Reference Links