-
Notifications
You must be signed in to change notification settings - Fork 22
Stage 0: Loops #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Adrianamm
wants to merge
20
commits into
frcsoftware:main
Choose a base branch
from
Adrianamm:fruitloops2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Stage 0: Loops #134
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
07c418a
hi
Adrianamm 8905c69
I forgot a space
Adrianamm d665631
fixed maybe
Adrianamm ca704d9
added suggestions
Adrianamm 3afa8fb
added ignore
Adrianamm 564e650
Merge branch 'main' into fruitloops2
Adrianamm fbef351
Removed duplicate lockfile key, reverted package upgrades
httphypixelnet b6a7846
Merge pull request #1 from httphypixelnet/fruitloops2
Adrianamm 5fc09b3
fixed ignore
Adrianamm 537dec2
added ignore correctly
Adrianamm bcb43f1
Merge branch 'main' into fruitloops2
Adrianamm 2ba6756
Merge branch 'main' into fruitloops2
Adrianamm 8eae64c
Merge branch 'main' into fruitloops2
Adrianamm 5c19510
applied suggestions
Adrianamm 3c3002a
Merge branch 'main' into fruitloops2
Adrianamm efb7579
Merge branch 'main' into fruitloops2
zachwaffle4 45d0aee
fix pr
zachwaffle4 9471f82
fix loops snippets
zachwaffle4 302d88f
added suggestions
Adrianamm c06e509
updated from feedback
Adrianamm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| class Drivetrain { | ||
| public void setThrottle(double speed) {} | ||
| } | ||
|
|
||
| boolean condition = false; | ||
| Drivetrain drivetrain = new Drivetrain(); | ||
|
|
||
| void main() { | ||
| // [whileSyntax] | ||
| while (condition) { | ||
| // code to run when condition is true | ||
| } | ||
| // [/whileSyntax] | ||
|
|
||
| { | ||
| // [whileExample] | ||
| int i = 0; | ||
| while (i < 6) { | ||
| System.out.println(i); // prints 0, 1, 2, 3, 4, 5 | ||
| i++; | ||
| } | ||
| // [/whileExample] | ||
| } | ||
|
|
||
| // [whileExample2] | ||
| int autoTimer = 0; | ||
| while (autoTimer <= 15){ | ||
| System.out.println("AutoMode is happening"); | ||
| autoTimer++; | ||
| } | ||
| // [/whileExample2] | ||
|
|
||
|
|
||
| { | ||
| // [ForExample1] | ||
| int i = 0; | ||
| while (i < 6) { | ||
| System.out.println("Hi!"); | ||
| i++; | ||
| } | ||
| // [/ForExample1] | ||
| } | ||
|
|
||
| // [ForExample2] | ||
| for (int i = 0; i < 6; i++) { | ||
| System.out.println("Hi!"); | ||
| } | ||
| //[/ForExample2] | ||
|
|
||
| // | ||
| // [forExample] | ||
| for (int i = 0; i < 5; i++){ | ||
| System.out.println(i); // prints 0, 1, 2, 3, 4 | ||
| } | ||
| // [/forExample] | ||
|
|
||
| if (false) { | ||
| // [Infinite1] | ||
| int timer = 0; | ||
| while (timer < 7){ | ||
| drivetrain.setThrottle(1); // sets drive motors to full speed | ||
| } | ||
| // [/Infinite1] | ||
| } | ||
|
|
||
| // [Infinite2] | ||
| int timer = 0; | ||
| while (timer < 7){ | ||
| drivetrain.setThrottle(1); // sets drive motors to full speed | ||
| timer++; // increments timer by 1 | ||
| } | ||
| // [/Infinite2] | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| --- | ||
| title: Loops | ||
| description: An Intro loops in Java | ||
| prev: learning-course/stage0/conditionals | ||
| next: learning-course/stage0/classes-methods | ||
| codeRegionSources: | ||
| default: stage0/snippets/src/Loops.java | ||
| --- | ||
|
|
||
| Oftentimes we want to do the same action multiple times in a row. | ||
| For instance, if you know your grades are about to be released, you might repeatedly check them until they come out. | ||
| In Java we can use loops to do this. | ||
|
|
||
| In this lesson, we're going to cover two main types of loops: **while** loops and **for** loops. | ||
|
|
||
| <Aside type="note"> | ||
| There is another type of loop, the do-while loop. We will not be covering it | ||
| because it isn't commonly used in FRC. However, if you want to learn more | ||
| about do while loops, you can learn more on w3school's page on [do while | ||
| loops](https://www.w3schools.com/java/java_while_loop_do.asp) | ||
| </Aside> | ||
|
|
||
| Conditionals and loops may seem similar but they have different purposes. | ||
| Conditionals are used to make decisions based on whether the condition is true or false. | ||
| Loops are used to repeat code until the condition is met. | ||
| This can be thought of conditionals make decisions, and loops are for repeating. | ||
|
|
||
| ## while | ||
|
|
||
| `while` loops are the most simple type of loops. | ||
| If you have used block coding before, this is similar to the repeat block. | ||
| If you have taken an CS class, you might already be familiar with a `while` loop. | ||
|
|
||
| The syntax of a `while` loops is as shown: | ||
|
|
||
| ```java #whileSyntax | ||
|
|
||
| ``` | ||
|
|
||
| You might notice that a `while` loop looks very similar to the `if` conditional, which was discussed in the previous lesson. | ||
| This is because the `while` loop and `if` statement are both based around checking a condition, and then performing an action depending on whether the condition is true or false. | ||
| A `while` loop includes: | ||
|
|
||
| - the `while` keyword to declare that we're making a `while` loop | ||
| - `( )` to hold the condition that is either true or false. | ||
| Example: (5 > 1) is a condition and it is true because 5 is greater than 1 | ||
| - `{` to denote the opening of the loop's code block | ||
| - `}` to denote the end of the loop's code block | ||
|
|
||
| The following flowchart `demonstrates` how a while loop works: | ||
|
|
||
| <ContentFigure | ||
| src="/learning-course/stage0/loops/WhileLoop.webp" | ||
| width="300px" | ||
| height="300px" | ||
| /> | ||
|
|
||
| Like the `if` conditional, the `while` loop will run its code block if the condition is true. | ||
| Unlike the `if` conditional, however, when the code block is done, the `while` loop will evaluate its condition again. | ||
| If the condition is still true, the code block will run again. | ||
| This will repeat until the condition is false. | ||
| Also unlike the `if` conditional, a `while` loop (or any other type of loop) can't have any `else` statements attached. | ||
| This means that if the condition is false when the loop is first encountered, the entire thing is skipped. | ||
|
|
||
| Let's look at an example: | ||
|
|
||
| ```java #whileExample | ||
|
|
||
| ``` | ||
|
|
||
| In the example, there is an `int` called `i` which holds the value 0. The `while` loop has the condition `i < 6`. | ||
| First, the compiler checks if the condition is true. | ||
| We know that 0 is less than 6 so the `while` code block runs and the value of `i`, which is currently 0, is printed to the terminal. | ||
| The value of `i` is also updated because of `i++` and it changes to 1. | ||
| Now the condition checks if 1 is less than 6. We know that it is true, so the value of `i` gets printed and the value of `i` is increased by 1. | ||
| This repeats and eventually `i`'s value becomes 6. | ||
| Now, when the condition is evaluated, it's `false`, as 6 is not less than 6, so the loop is complete. | ||
|
|
||
| <Aside type="tip"> | ||
| Does the ``i++`` look familiar? This was covered in the operator section. If | ||
| you need a refresher on what an operator is and how it works, check out the | ||
| operator page. | ||
| </Aside> | ||
|
|
||
| Lets look at an another example! | ||
| In the example below we have: | ||
|
|
||
| ```java #whileExample2 | ||
|
|
||
| ``` | ||
|
|
||
| Without running the code, what do you think happens? | ||
|
|
||
| `autoTimer` is set to 0. The `while` loop's condition is `autoTimer <= 15`. | ||
| 0 is less than 15 so the code enters the `while` code block, and prints outs `Auto is happening`. | ||
| `autoTimer++` is run which increases `autoTimer`'s value by one. | ||
|
Adrianamm marked this conversation as resolved.
|
||
| The code goes back to the condition, 1 is less than 15, so the code enters the `while` code block again, printing out `Auto is happening`. | ||
| Like before, `autoTimer++` runs which increases `autoTimer` by one again making the value be 2. This repeats until `autoTimer` is 16. When that happens the condition is false and the `while` loop is skipped. | ||
|
|
||
| ## for | ||
|
|
||
| With the power of while loops, we're able to repeat a block of code any number of times, like so: | ||
|
|
||
| ```java #ForExample1 | ||
|
|
||
| ``` | ||
|
|
||
| This loop depends on 3 important statements: | ||
| `int i = 0;`, `i < 6`, and `i++`. | ||
| For loops allow us to inline these statements: | ||
|
|
||
| ```java #ForExample2 | ||
|
|
||
| ``` | ||
|
|
||
| Using the previous, we can see that the `for` loop syntax includes: | ||
|
|
||
| - the `for` keyword to declare that we're making a `for` loop | ||
| - `()`to hold the three statements which allow the loop to run | ||
| - `{` to denote the opening of the loop's code block | ||
| - `}` to denote the ending of the loop's code block | ||
|
|
||
| The three statements are | ||
|
|
||
| - **initialization**: creates a variable that sets the starting point of the loop. | ||
| This is the `int i = 0;` seen in the previous example. | ||
| - **condition**: a condition, which uses the variable, that is checked before each iteration. | ||
| This is `i < 6;`. | ||
| If the condition is true, the code inside the loop runs | ||
| - **update**: updates the variable created in initialization after each iteration. | ||
| Lastly, that's `i++`; | ||
|
|
||
| Let's look at another example: | ||
|
|
||
| ```java #forExample | ||
|
|
||
| ``` | ||
|
|
||
| In this example, we create a variable `i` that is set to 0. When `i` is less than 5, the `for` code block runs, printing the value of `i` which is 0. | ||
| Then it goes back to the update statement which is `i++`, `i` has increased by 1 and `i`'s value is now 1. | ||
| We check the condition, 1 is less than 5, so `for` code block runs, the new value of `i` is printed, `i` is updated and this repeats until `i` is no longer less than 5. | ||
| When `i` is no longer less than 5, the `for` loop is skipped. | ||
|
|
||
| ## Be Careful about Infinite Loops | ||
|
|
||
| It is important to make sure that the condition can become false. | ||
|
Adrianamm marked this conversation as resolved.
|
||
| If the condition is always true, the loop will always run causing an infinite loop. | ||
| Infinite loops can be prevented by ensuring that the value inside the condition changes by incrementing or decrementing. | ||
|
|
||
| <Aside type="note"> | ||
|
|
||
| When diving into FRC programming, you will find that infinite loops are occasionally encouraged (such as in commands). | ||
| This will be talked about more in Stage 1 | ||
|
|
||
| </Aside> | ||
|
|
||
| ```java #Infinite1 | ||
|
|
||
| ``` | ||
|
|
||
| In this example, we want our drive train to run for 7 seconds. | ||
| We have our variable called `timer` which is set to 0. | ||
| 0 is less than 7 so the code inside runs and the robot drives forward at full speed. | ||
| However, `timer`'s value never changes. | ||
| This means `timer` will always be 0. 0 is less than 7 so the loop will continuously run and the robot never stops driving! | ||
|
|
||
| To fix this, we would make sure to increment the timer value as shown below. | ||
|
|
||
| ```java #Infinite2 | ||
|
|
||
| ``` | ||
|
|
||
| `timer` now increments by 1 each time the loop is run, and the robot will stop driving after 7 seconds. | ||
|
Adrianamm marked this conversation as resolved.
|
||
|
|
||
| ## Loops Exercise | ||
|
|
||
| <Aside type="exercise"> | ||
|
|
||
| WIP | ||
|
|
||
| </Aside> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.