-
Notifications
You must be signed in to change notification settings - Fork 16
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
base: main
Are you sure you want to change the base?
Stage 0: Loops #134
Changes from all commits
07c418a
8905c69
d665631
ca704d9
3afa8fb
564e650
fbef351
b6a7846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| boolean condition = true; | ||
| int statement1 = 0; | ||
| boolean statement2 = true; | ||
| int statement3 = 1; | ||
|
|
||
|
|
||
| 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 autoTime = 0; | ||
| while (autoTime <= 15){ | ||
| System.out.println("AutoMode is happening"); | ||
| autoTime++; | ||
| } | ||
| // [/whileExample2] | ||
|
|
||
| // [forSyntax] | ||
| rli:ignore | ||
| for (statement1; statement2; statement3) { | ||
| // code to run when statement 2 is true | ||
| } | ||
| // [/forSyntax] | ||
|
|
||
| // [forExample] | ||
| for (int i = 0; i < 5; i++){ | ||
| System.out.println(i); // prints 0, 1, 2, 3, 4 | ||
| } | ||
| // [/forExample] | ||
|
|
||
| // [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] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| --- | ||
| title: Loops | ||
| description: An Intro loops in Java | ||
| prev: false | ||
| next: false | ||
| codeRegionSources: | ||
| default: stage0/snippets/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. | ||
|
|
||
| We will be going over two types of loops which are: | ||
|
|
||
| - while | ||
| - for | ||
|
|
||
| <Aside type="note"> | ||
| There is another type of loop called do while and because it's not commonly | ||
| used in FRC we will not be covering it. 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 has a similar syntax to conditionals, which was discussed in the previous section. | ||
| A `while` loop's syntax includes: | ||
|
|
||
| - `while` is a keyword | ||
| - `( )` holds the condition that is either true or false. | ||
| Example: (5 > 1) is a condition and it is true because 5 is greater than 1 | ||
| - `{` an open bracket is used to denote the opening of the loop | ||
| - `}` a closed bracket is used to denote the end of the loop | ||
|
|
||
| Another way of understanding how a `while` loop works is with the flow chart below | ||
|
|
||
| <ContentFigure src="/loops/WhileLoop.webp" width="400px" height="800px" /> | ||
|
|
||
| Similar to conditionals, a `while` loop will run the code block inside the `while` loop when the condition is true. | ||
| After the code block is run, it goes back to the condition. | ||
| If the condition is true, the code block will run again. | ||
| This will repeat until the condition is false. | ||
| When it's false, the loop will be skipped. | ||
| To understand how this works, 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. | ||
| 6 is not less than 6, therefore the loop is done and the `while` code block does not run. | ||
|
|
||
| <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? | ||
|
|
||
| `autoTime` is set to 0. The `while` loop's condition is `autoTime <= 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. | ||
| 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 `autoTime` is 15. When that happens the condition is false and the `while` loop is skipped. | ||
|
|
||
| ## for | ||
|
|
||
| Sometimes, we might want code that loops an exact number of times. | ||
| This can be done with a `for` loop. | ||
| The syntax of a `for` is as follows: | ||
|
|
||
| {/* rli:ignore */} | ||
|
|
||
| ```java #forSyntax | ||
|
|
||
| ``` | ||
|
|
||
| In the syntax: | ||
|
|
||
| - `for` is a keyword | ||
| - `()` holds the three statements | ||
| - `{` an open bracket is used to denote the opening of the loop | ||
| - `}` a closed bracket is used to denote the end of the loop | ||
|
|
||
| The three statements are | ||
|
|
||
| - initialization: creates a variable that sets the starting point of the loop. | ||
| - condition: a condition, which uses the variable, that is checked before each iteration. | ||
| If the condition is true, the code inside the loop runs | ||
| - update: updates the variable created in initialization after each iteration | ||
|
|
||
| The best way to explain how `for` loops work is with an 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. | ||
|
|
||
| Another way of understanding how a for loop works is with the flow chart below | ||
|
|
||
| <ContentFigure src="/loops/Forloop.webp" width="400px" height="800px" /> | ||
|
|
||
| ## Be Careful about Infinite Loops | ||
|
|
||
| It is important to make sure that the condition can become false. | ||
| 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. | ||
|
|
||
| ```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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure how much nuance is needed here, but I hesitate on this example because it's 7 loops, not seconds, which could be confusing when they try to write the same code later and it doesn't wait for 7 seconds. would appreciate someone else's input here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a normal not robot example be better? So just a while loop that counts up to 5 |
||
|
|
||
| ## Loops Exercise | ||
|
|
||
| <Aside type="exercise"> | ||
|
|
||
| WIP | ||
|
|
||
| </Aside> | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Not sure how to do it, but it would be nice if the
rli:ignorewouldn't show up in the final code block. Maybe if it goes above the comment?