Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • Else-If Statements
  • Else Statements
  1. Intro to Java
  2. Fundimentals of Java

If Statements and Conditions

Ever needed your code to be smart for once? You can use If Statements!

If statements run a block of code depending on if a condition (boolean) is true. Here's an example code.

int num = 3;

if (num > 2) {
   System.out.println("3 is in fact greater than 2.");   
    
}
/* output:
 * 3 is in fact greater than 2.
 */

For example, in the code above, if the condition inside the parameters, num > 2 (num = 3) is true, the print statement will run. Now, let's say we changed the value of i to something like 1.

int num = 1;

if (num > 2) {
   System.out.println("3 is in fact greater than 2.");   
    
}
/* output:
 * 
 */

Else-If Statements

Notice how the code doesn't print anything. Now, what if you wanted to check if num was greater than 2 and smaller than- let's say 7. We can do this:

int num = 6;

if (num > 2) {
   System.out.println("3 is in fact greater than 2.");   
    
} else if (num < 7) {
   System.out.println("num is probably greater than 7");   
   
}
/* output:
 * num is probably greater than 7
 */

Else-if statements are similar to if-statements but run when the previous conditions are false. They provide additional criteria for the code. In the code above, the else-if statement will run if num is greater than 2 and less than 7.

You can stack else-if statements, creating a chain of if/else-if statements. These are useful to check for a bunch of different scenarios.

int num = 10;

if (num > 2) {
   System.out.println("3 is in fact greater than 2.");     
    
} else if (num < 7) {
   System.out.println("num is probably greater than 7");   
   
} else if (num < 25) {
   System.out.println("help me i cant think of anything mildly interesting for this part");  
   
}

/* output:
 * help me i cant think of anything mildly interesting for this part
 */

However- what if all of these conditions were false? Use an else statement!

Else Statements

Else statements are like the catch-all statement. If somehow the rest of the if statements don't run, this is run. (If you want to think of it like this, else statements are basically else if (true))

int num = 10;

if (num > 2) {
   System.out.println("3 is in fact greater than 2.");     
    
} else if (num < 7) {
   System.out.println("num is probably greater than 7");   
   
} else if (num < 25) {
   System.out.println("help me i cant think of anything mildly interesting for this part");  
   
} else {
   System.out.println("wow number is pretty big or pretty small i think");   
   
}

/* output:
 * wow number is pretty big or pretty small i think
 */

As you can see, the else-statement is ran instead of the other conditions, as the other ones were false. If you want to play around with this code for a bit, I made a link

PreviousCommentsNextBoolean Zen

Last updated 2 months ago

🟩