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
  • While Loops
  • Counter
  • Goal
  • Updater
  • Side Tangent— break
  • Challenge— Recreate a 2010 YouTube Comments Section (NOTE: I'M GOING TO CHANGE THIS TO SOMETHING ACTUALLY USEFUL)
  1. Intro to Java
  2. Fundimentals of Java

Loops

Ever needed to repeat a set of code? Were you tempted to do something like this?

int var = 0;

System.out.println(var);
var++;
System.out.println(var);
var++;
System.out.println(var);
var++;
System.out.println(var);
var++;
System.out.println(var);
var++;
System.out.println(var);
var++;
System.out.println(var);
var++;
/* [you get the point] */

As you can tell, this is hideous. Programmers generally make their code as simple and easily modifiable as possible— and this isn't either.

This is what loops are for! Loops repeat a set of code any number of times. You can remake all of that code with five lines of code.

int var = 0;
while (var < 9) {
    System.out.println(var);
    var += 1;
}

This specific one is a while loop. Conveniently we'll go over it right below!

While Loops

Let's start this off with the simplest— while loops. While loops loop while a condition is true. They're basically if-statements but instead of running once, they repeat their code until the condition is false.

Here's the syntax of a while loop:

while (condition) {
    /* [code] */
}

condition- a boolean. When whatever's in here is true, the loop will repeat.

[This applies to all loops]. If your condition is never reached, your code will not stop looping. And if the condition is met before your loop even gets a chance to run, it won't do anything.

For loops are like while loops, but their condition is slightly different. Notice back up in the example, where I had to manually declare the variable var. For loops do it for us!

The syntax of a for loop is:

for (counter, goal, update) {
    /* [your code here] */
    

There are three parts of a for loop:

Counter

This part runs right before the loop begins, and the for loop uses it to count how many times it ran. Because it's a variable, you can access it and do cool things with it. However, you can only access the variable from inside the for loop, and it's unusable after the loop is finished.

When naming the for loop's variable, use letters like i, j, k, etc. (chars are used as names for temporary variables like this one)

Goal

All goal holds is a boolean. It's like the condition part back up in the while loop. Unlike while loops, we use this to check if the counteris a specific value.

Updater

The updater runs after the loop iterates (repeats). Its main purpose is to update the Countervariable, and make sure that it reaches the goal.

If we go back to that example of the while loop, we can create one using a for loop, but with even less lines of code! It also looks a lot cleaner!

for (i = 0; i < 9; i++) {
    System.out.println(i);
}

If you want an explanation, we're setting a counter variable i to 0. Every time the loop runs, we check if this variable is less than 9. To make sure that the loop stops somehow, we'll add 1 to it every time it loops.

Side Tangent— break

When you call break in a loop, the code stops the code no matter what. Even if the code hasn't finished running its body code, it will ignore the rest of the body code.

Break is generally used as a last-resort, when something wrong happened in your code like an infinite loop. Using break improperly will also s̵u̶mĢ·mĢøoĢ·nĢø tĢ·Ģ•ĢŒĢ¦Ģ©Ģ™hĢ¶ĢĢ£eĢ¶ĶĢ¾ĶĢ–Ģ» šŒāƒ„āƒ’Ģøš”āƒ„āƒ’Ģøš‹āƒ„āƒ’ĢøšŠāƒ„āƒ’Ģø into this realm.

Challenge— Recreate a 2010 YouTube Comments Section (NOTE: I'M GOING TO CHANGE THIS TO SOMETHING ACTUALLY USEFUL)

I'm feeling pretty nostalgic today, so I'll make you recreate a 2010 YouTube comments section. And by that I mean spam the "This is Bob" copypasta like below.

Your code will require:

Here's what a comment would look like.

XxX_MLGGAMERPRO_XxX  ☻/ This is bob. Copy and paste him so he can take over youtube
15 years ago        (ā–Œ
                    /\

Solution
private void printYoutubeComments(int count) {
    for (int i = 0; i < count; i += 1) { 
        System.out.println("XxX_MLGGAMERPRO_XxX  ☻/ This is bob. Copy and paste him so he can take over youtube");
        System.out.println("15 years ago        (ā–Œ");
        System.out.println("                    /\");
    }
}

PreviousBoolean ZenNextChallenge- Create a Box House

Last updated 2 months ago

🟩