|
236 | 236 | */ |
237 | 237 |
|
238 | 238 | /** |
239 | | - * <a href="#/p5/for">for</a> creates a loop that is useful for executing the code multiple times. |
| 239 | + * <a href="#/p5/for">for</a> creates a loop that is useful for executing one section of code multiple times. |
240 | 240 | * |
241 | 241 | * A 'for loop' consists of three different expressions inside of a parenthesis, all of which are optional. |
242 | 242 | * These expressions are used to control the number of times the loop is run. |
|
248 | 248 | * The code inside of the loop body (in between the curly braces) is executed between the evaluation of the second |
249 | 249 | * and third expression. |
250 | 250 | * |
| 251 | + * As with any loop, it is important to ensure that the loop can 'exit', or that |
| 252 | + * the test condition will eventually evaluate to false. The test condition with a <a href="#/p5/for">for</a> loop |
| 253 | + * is the second expression detailed above. Ensuring that this expression can eventually |
| 254 | + * become false ensures that your loop doesn't attempt to run an infinite amount of times, |
| 255 | + * which can crash your browser. |
| 256 | + * |
251 | 257 | * From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for">the MDN entry</a>: |
252 | 258 | * Creates a loop that executes a specified statement until the test condition evaluates to false. |
253 | 259 | * The condition is evaluated after executing the statement, resulting in the specified statement executing at least once. |
|
263 | 269 | * </div> |
264 | 270 | */ |
265 | 271 |
|
| 272 | +/** |
| 273 | + * <a href="#/p5/while">while</a> creates a loop that is useful for executing one section of code multiple times. |
| 274 | + * |
| 275 | + * With a 'while loop', the code inside of the loop body (in between the curly braces) is run repeatedly until the test condition |
| 276 | + * (inside of the parenthesis) evaluates to false. Unlike a <a href="#/p5/for">for</a> loop, the condition is tested before executing the code body with <a href="#/p5/while">while</a>, |
| 277 | + * so if the condition is initially false the loop body, or statement will never execute. |
| 278 | + * |
| 279 | + * As with any loop, it is important to ensure that the loop can 'exit', or that |
| 280 | + * the test condition will eventually evaluate to false. This is to keep your loop from trying to run an infinite amount of times, |
| 281 | + * which can crash your browser. |
| 282 | + * |
| 283 | + * From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while">the MDN entry</a>: |
| 284 | + * The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. |
| 285 | + * The condition is evaluated before executing the statement. |
| 286 | + * @property while |
| 287 | + * |
| 288 | + * @example |
| 289 | + * <div class='norender'> |
| 290 | + * <code> |
| 291 | + * // This example logs the lines below to the console |
| 292 | + * // 4 |
| 293 | + * // 3 |
| 294 | + * // 2 |
| 295 | + * // 1 |
| 296 | + * // 0 |
| 297 | + * let num = 5; |
| 298 | + * while (num > 0) { |
| 299 | + * num = num - 1; |
| 300 | + * console.log(num); |
| 301 | + * } |
| 302 | + * </code> |
| 303 | + * </div> |
| 304 | + */ |
| 305 | + |
266 | 306 | /** |
267 | 307 | * @submodule JS Method |
268 | 308 | */ |
|
0 commit comments