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
  • Item
  • Collection
  1. Intro to Java
  2. Advanced Concepts

For-Each Loops

For-Each loops are different from the other loops I mentioned in Loops, as they are specifically designed for traversing an array (accessing every element in an array).

Let's say this was an array that you wanted to traverse through.

int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};

If you had a for-each loop that you wanted to use to traverse this away, it'll access each element left to right. For example, if I were to use a for-each loop and

for (int num : arr) {
   System.out.println(i + ", "); /* output: 1, 2, 3, 4, 5, 6, 7, 8, */
   
}

Here's its syntax:

for (Object item : Object[] collection) {
    [code]
}

Item

This is the current item that the for-each loop is on. However, you can only access its valueβ€” you can't change it or anything.

Collection

This is just the collection that the for-each loop is traversing through. The collection can be basically anything that can hold more than one value.

If you are using a for-each loop on a collection, do NOT modify it as you're traversing through the collection or you'll have a fun time dealing with logic errors.

PreviousErrorsNextObject Oriented Programming

Last updated 2 months ago

🟨