You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
63
57
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:
66
62
67
63
```java #whileExample
68
64
@@ -74,7 +70,7 @@ We know that 0 is less than 6 so the `while` code block runs and the value of `i
74
70
The value of `i` is also updated because of `i++` and it changes to 1.
75
71
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.
76
72
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.
78
74
79
75
<Asidetype="tip">
80
76
Does the ``i++`` look familiar? This was covered in the operator section. If
@@ -91,11 +87,20 @@ In the example below we have:
91
87
92
88
Without running the code, what do you think happens?
93
89
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.
99
104
100
105
## for
101
106
@@ -113,23 +118,26 @@ For loops allow us to inline these statements:
113
118
114
119
```
115
120
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:
117
122
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
122
127
123
128
The three statements are
124
129
125
130
-**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.
127
132
-**condition**: a condition, which uses the variable, that is checked before each iteration.
128
133
This is `i < 6;`.
129
134
If the condition is true, the code inside the loop runs
130
135
-**update**: updates the variable created in initialization after each iteration.
131
136
Lastly, that's `i++`;
132
137
138
+
All 3 statements are optional, but the semicolons must be present.
139
+
However, the examples in this section will use all statements.
140
+
133
141
Let's look at another example:
134
142
135
143
```java #forExample
@@ -145,6 +153,9 @@ When `i` is no longer less than 5, the `for` loop is skipped.
145
153
146
154
It is important to make sure that the condition can become false.
147
155
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.
148
159
Infinite loops can be prevented by ensuring that the value inside the condition changes by incrementing or decrementing.
149
160
150
161
<Asidetype="note">
@@ -154,8 +165,15 @@ This will be talked about more in Stage 1
154
165
155
166
</Aside>
156
167
157
-
```java #Infinite1
168
+
{/* rli:ignore */}
158
169
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
+
}
159
177
```
160
178
161
179
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
166
184
167
185
To fix this, we would make sure to increment the timer value as shown below.
168
186
169
-
```java #Infinite2
187
+
{/* rli:ignore */}
170
188
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
+
}
171
195
```
172
196
173
197
`timer` now increments by 1 each time the loop is run, and the robot will stop driving after 7 seconds.
0 commit comments