How to Use an If-Else Statement in VEX C++

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 "robot-config.h"
int main(){
    //The robot moves forward if the Bumper Switch is held down when the program starts.
    //Otherwise, nothing happens.
    if(Bumper.pressing()){
        LeftMotor.spin(directionType::fwd);
        RightMotor.spin(directionType::fwd);
    }
}

Step 2: Add a forever loop in programs that should check conditions repeatedly

  • Add a while(true) loop around the if 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 VEX C++ for more information.

NOTE:   Use   //   notation  to include comments that explain what that section of code does. In this example, the comments explain: 1) The robot will continually check if the Bumper Switch is pressed and spin the robot's motors forward if it is, and 2) The robot will not stop moving forward once started.

Code that can be copied and pasted:

#include "robot-config.h"

int main(){
    //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(directionType::fwd);
            RightMotor.spin(directionType::fwd);
        }
    }
}

Step 3: Finish the else part

  • Type in else after the closing curly brace }  of the if 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: 1) The robot will continually check if the Bumper Switch is pressed and spin the robot's motors forward if it is, and 2) The robot's motors will stop when the Bumper Switch is no longer pressed.

Code that can be copied and pasted:

#include "robot-config.h"

int main(){
    //The robot continually checks if the Bumper Switch is pressed and runs the robot forward if the Bumper Switch is pressed. 
    //It stops the motors if the Bumper Switch is not pressed.
    while(true){
        if(Bumper.pressing()){
            LeftMotor.spin(directionType::fwd);
            RightMotor.spin(directionType::fwd);
        }else{
            LeftMotor.stop();
            RightMotor.stop();
        }
    }
}

Still need help? Contact Us Contact Us