Step 1: Write the if
part
- Type in
if
and add the condition that the program should check for within its parentheses( )
.
NOTE: In this example, the condition is that the Bumper Switch is being pressed.
- Inside the
if
statement's curly braces{ }
, add command(s) for the robot to carry out if that condition is met.
NOTE: In this example, the commands are to spin both motors forward.
NOTE: Use //
notation to include comments that explain what that section of code does. In this example, the comments explain the robot's two conditions: 1)The Bumper Switch is pressed and the motors spin the robot forward or 2) nothing happens.
Code that can be copied and pasted:
#include "vex.h" using namespace vex; int main() { // Initializing Robot Configuration. DO NOT REMOVE! vexcodeInit(); // The robot moves forward if the Bumper Switch is held down when the program starts. // Otherwise, nothing happens. if(Bumper.pressing()){ LeftMotor.spin(forward); RightMotor.spin(forward); } }
Step 2: Add a forever loop in programs that should check conditions repeatedly
- Add a
while(true)
loop around theif
statement of the program. It will have the program check if the condition is true continuously. - Adjust the indents on the lines of code to keep your program organized.
NOTE: If the program should only check the condition once, then a loop is not necessary.
NOTE: This example requires a loop because the robot should check if the Bumper Switch is pressed at any time. See How to Program with a While Loop in VEXcode Pro V5 for more information.
NOTE: Use //
notation to include comments that explain what that section of code does. In this example, the comments explain:
- The robot will continually check if the Bumper Switch is pressed and spin the robot's motors forward if it is.
- The robot will not stop moving forward once started.
Code that can be copied and pasted:
#include "vex.h" using namespace vex; int main() { // Initializing Robot Configuration. DO NOT REMOVE! vexcodeInit(); // The robot continually checks if the Bumper Switch is pressed and runs the robot forward if the Bumper Switch is pressed. // However, it will never stop spinning the motors. while(true){ if (Bumper.pressing()){ LeftMotor.spin(forward); RightMotor.spin(forward); } } }
Step 3: Finish the else
part
- Type in
else
after the closing curly brace}
of theif
statement. - Inside the
else
statement's curly braces{ }
, add command(s) for the robot to carry out whenever the condition is not met.
NOTE: In this example, the motors stop when the Bumper Switch is not pressed.
NOTE: If the program does not need to do something 'else', an if
statement can be used without the else
.
NOTE: Use //
notation to include comments that explain what that section of code does. In this example, the comments explain:
- The robot will continually check if the Bumper Switch is pressed and spin the robot's motors forward if it is.
- The robot's motors will stop when the Bumper Switch is no longer pressed.
Code that can be copied and pasted:
#include "vex.h" using namespace vex; int main() { // Initializing Robot Configuration. DO NOT REMOVE! vexcodeInit(); // The robot moves forward if the Bumper Switch is held down when the program starts. // Otherwise, nothing happens. if(Bumper.pressing()){ LeftMotor.spin(forward); RightMotor.spin(forward); }else{ LeftMotor.stop(); RightMotor.stop(); } }