Skip to content

Commit ccfd28a

Browse files
committed
add while reference and warning about infinite loops
1 parent 1a33a34 commit ccfd28a

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/core/reference.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
*/
237237

238238
/**
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.
240240
*
241241
* A 'for loop' consists of three different expressions inside of a parenthesis, all of which are optional.
242242
* These expressions are used to control the number of times the loop is run.
@@ -248,6 +248,12 @@
248248
* The code inside of the loop body (in between the curly braces) is executed between the evaluation of the second
249249
* and third expression.
250250
*
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+
*
251257
* From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for">the MDN entry</a>:
252258
* Creates a loop that executes a specified statement until the test condition evaluates to false.
253259
* The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.
@@ -263,6 +269,40 @@
263269
* </div>
264270
*/
265271

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+
266306
/**
267307
* @submodule JS Method
268308
*/

0 commit comments

Comments
 (0)