Skip to content

Commit bd516df

Browse files
committed
Merge remote-tracking branch 'upstream/main' into stage0/interfaces-data-structures
# Conflicts: # examples/stage0/snippets/src/Loops.java # src/content/docs/learning-course/stage0/loops.mdx
2 parents 1a6db9d + 72eed43 commit bd516df

2 files changed

Lines changed: 72 additions & 63 deletions

File tree

examples/stage0/snippets/src/Loops.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ void main() {
2929
}
3030

3131
// [whileExample2]
32-
int autoTimer = 0;
33-
while (autoTimer <= 15){
34-
System.out.println("AutoMode is happening");
35-
autoTimer++;
32+
int timer = 0;
33+
34+
while (timer <= 5) {
35+
if (timer < 5) {
36+
System.out.println("Drive Forward");
37+
} else {
38+
System.out.println("Stop Driving");
39+
}
40+
timer++;
3641
}
3742
// [/whileExample2]
3843

@@ -53,29 +58,9 @@ void main() {
5358
}
5459
//[/ForExample2]
5560

56-
//
5761
// [forExample]
5862
for (int i = 0; i < 5; i++){
5963
System.out.println(i); // prints 0, 1, 2, 3, 4
6064
}
6165
// [/forExample]
62-
63-
if (false) {
64-
// [Infinite1]
65-
int timer = 0;
66-
while (timer < 7){
67-
drivetrain.setThrottle(1); // sets drive motors to full speed
68-
}
69-
// [/Infinite1]
70-
}
71-
72-
{
73-
// [Infinite2]
74-
int timer = 0;
75-
while (timer < 7) {
76-
drivetrain.setThrottle(1); // sets drive motors to full speed
77-
timer++; // increments timer by 1
78-
}
79-
// [/Infinite2]
80-
}
81-
}
66+
}

src/content/docs/learning-course/stage0/loops.mdx

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,58 @@ codeRegionSources:
77
default: stage0/snippets/src/Loops.java
88
---
99

10-
Oftentimes we want to do the same action multiple times in a row.
10+
Oftentimes in code, there is a need to repeat the same action multiple times in a row.
1111
For instance, if you know your grades are about to be released, you might repeatedly check them until they come out.
1212
In Java we can use loops to do this.
1313

14-
We will be going over two types of loops which are:
15-
16-
- while
17-
- for
14+
In this lesson, we're going to cover two main types of loops: **while** loops and **for** loops.
1815

1916
<Aside type="note">
20-
There is another type of loop called do while and because it's not commonly
21-
used in FRC we will not be covering it. However, if you want to learn more
17+
There is another type of loop, the do-while loop. We will not be covering it
18+
because it isn't commonly used in FRC. However, if you want to learn more
2219
about do while loops, you can learn more on w3school's page on [do while
2320
loops](https://www.w3schools.com/java/java_while_loop_do.asp)
2421
</Aside>
2522

2623
Conditionals and loops may seem similar but they have different purposes.
27-
Conditionals are used to make decisions based on whether the condition is true or false.
28-
Loops are used to repeat code until the condition is met.
29-
This can be thought of conditionals make decisions, and loops are for repeating.
24+
Conditionals and loops both control the flow of programs.
25+
Conditionals are used to run a block of code once, whereas a loop can run a block of multiple times
3026

3127
## while
3228

3329
`while` loops are the most simple type of loops.
34-
If you have used block coding before, this is similar to the repeat block.
35-
If you have taken an CS class, you might already be familiar with a `while` loop.
3630

3731
The syntax of a `while` loops is as shown:
3832

3933
```java #whileSyntax
4034

4135
```
4236

43-
You might notice that a `while` loop has a similar syntax to conditionals, which was discussed in the previous section.
44-
A `while` loop's syntax includes:
37+
You might notice that a `while` loop looks very similar to the `if` conditional, which was discussed in the previous lesson.
38+
A `while` loop includes:
4539

46-
- `while` is a keyword
47-
- `( )` holds the condition that is either true or false.
40+
- the `while` keyword to declare that we're making a `while` loop
41+
- `( )` to hold the condition that is either true or false.
4842
Example: (5 > 1) is a condition and it is true because 5 is greater than 1
49-
- `{` an open bracket is used to denote the opening of the loop
50-
- `}` a closed bracket is used to denote the end of the loop
43+
- `{` to denote the opening of the loop's code block
44+
- `}` to denote the end of the loop's code block
5145

52-
Another way of understanding how a `while` loop works is with the flow chart below
46+
The following flowchart demonstrates how a while loop works:
5347

5448
<ContentFigure
5549
src="/learning-course/stage0/loops/WhileLoop.webp"
5650
width="300px"
5751
height="300px"
5852
/>
5953

60-
Similar to conditionals, a `while` loop will run the code block inside the `while` loop when the condition is true.
61-
After the code block is run, it goes back to the condition.
62-
If the condition is true, the code block will run again.
54+
Like the `if` conditional, the `while` loop will run its code block if the condition is true.
55+
Unlike the `if` conditional, however, when the code block is done, the `while` loop will evaluate its condition again.
56+
If the condition is still true, the code block will run again.
6357
This will repeat until the condition is false.
64-
When it's false, the loop will be skipped.
65-
To understand how this works, let's look at an example.
58+
Also unlike the `if` conditional, a `while` loop (or any other type of loop) can't have any `else` statements attached.
59+
This means that if the condition is false when the loop is first encountered, the entire thing is skipped.
60+
61+
Let's look at an example:
6662

6763
```java #whileExample
6864

@@ -74,7 +70,7 @@ We know that 0 is less than 6 so the `while` code block runs and the value of `i
7470
The value of `i` is also updated because of `i++` and it changes to 1.
7571
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.
7672
This repeats and eventually `i`'s value becomes 6.
77-
6 is not less than 6, therefore the loop is done and the `while` code block does not run.
73+
Now, when the condition is evaluated, it's `false`, as 6 is not less than 6, so the loop is complete.
7874

7975
<Aside type="tip">
8076
Does the ``i++`` look familiar? This was covered in the operator section. If
@@ -91,11 +87,20 @@ In the example below we have:
9187

9288
Without running the code, what do you think happens?
9389

94-
`autoTimer` is set to 0. The `while` loop's condition is `autoTimer <= 15`.
95-
0 is less than 15 so the code enters the `while` code block, and prints outs `Auto is happening`.
96-
`autoTimer++` is run which increases `autoTimer`'s value by one.
97-
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`.
98-
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.
90+
`timer` is set to 0, and the `while` loop’s condition is checking if `timer` is less than or equal to 5.
91+
0 is less than 5, so the code enters the while loop.
92+
Now the `if` statement checks whether `timer` is less than 5.
93+
0 is less than 5, so it prints out `Drive Forward`.
94+
The `if` statement is done, and the code runs `timer++`, which increases `timer`’s value by one.
95+
96+
The code goes back to the `while` loop.
97+
1 is less than 5, so the code enters the `while` loop again.
98+
For the `if` statement, 1 is less than 5.
99+
Therefore, `Drive Forward` gets printed out again.
100+
101+
This repeats 4 times until `timer` is 5.
102+
Once this happens, the `if` statement is skipped since 5 is not less than 5.
103+
Now the else runs and prints out `Stop Driving`, `timer`’s value increases to 6, 6 is not less than 5, so the code stops running.
99104

100105
## for
101106

@@ -113,23 +118,26 @@ For loops allow us to inline these statements:
113118

114119
```
115120

116-
Using the example above, we can see that the `for` loop syntax goes as follows:
121+
Using the previous example, we can see that the `for` loop syntax includes:
117122

118-
- `for` is a keyword
119-
- `()` holds the three statements
120-
- `{` an open bracket is used to denote the opening of the loop
121-
- `}` a closed bracket is used to denote the end of the loop
123+
- the `for` keyword to declare that we're making a `for` loop
124+
- `()`to hold the three statements which allow the loop to run
125+
- `{` to denote the opening of the loop's code block
126+
- `}` to denote the ending of the loop's code block
122127

123128
The three statements are
124129

125130
- **initialization**: creates a variable that sets the starting point of the loop.
126-
This is the `int i = 0;` seen in the example above.
131+
This is the `int i = 0;` seen in the previous example.
127132
- **condition**: a condition, which uses the variable, that is checked before each iteration.
128133
This is `i < 6;`.
129134
If the condition is true, the code inside the loop runs
130135
- **update**: updates the variable created in initialization after each iteration.
131136
Lastly, that's `i++`;
132137

138+
All 3 statements are optional, but the semicolons must be present.
139+
However, the examples in this section will use all statements.
140+
133141
Let's look at another example:
134142

135143
```java #forExample
@@ -145,6 +153,9 @@ When `i` is no longer less than 5, the `for` loop is skipped.
145153

146154
It is important to make sure that the condition can become false.
147155
If the condition is always true, the loop will always run causing an infinite loop.
156+
When an infinite loop happens, the code never stops.
157+
This can be unsafe if the code is running a robot mechanism.
158+
This could cause a robot to never stop driving forward or an arm to always move.
148159
Infinite loops can be prevented by ensuring that the value inside the condition changes by incrementing or decrementing.
149160

150161
<Aside type="note">
@@ -154,8 +165,15 @@ This will be talked about more in Stage 1
154165

155166
</Aside>
156167

157-
```java #Infinite1
168+
{/* rli:ignore */}
158169

170+
```java
171+
int timer = 0;
172+
if (false) {
173+
while (timer < 7){
174+
drivetrain.setThrottle(1); // sets drive motors to full speed
175+
}
176+
}
159177
```
160178

161179
In this example, we want our drive train to run for 7 seconds.
@@ -166,8 +184,14 @@ This means `timer` will always be 0. 0 is less than 7 so the loop will continuou
166184

167185
To fix this, we would make sure to increment the timer value as shown below.
168186

169-
```java #Infinite2
187+
{/* rli:ignore */}
170188

189+
```java
190+
int timer = 0;
191+
while (timer < 7){
192+
drivetrain.setThrottle(1); // sets drive motors to full speed
193+
timer++; // increments timer by 1
194+
}
171195
```
172196

173197
`timer` now increments by 1 each time the loop is run, and the robot will stop driving after 7 seconds.

0 commit comments

Comments
 (0)