JavaScript While Loop

Start Date: 01/24/2021

Course Type: Common Course

Course Link:

Coursera Plus banner featuring three learners and university partner logos

Course Tag

Related Wiki Topic

Article Example
JavaScript syntax The syntax of the JavaScript while loop is as follows:
While loop Also, in C and its descendants, a "while" loop is a "for" loop with no initialization or counting expressions, i.e.,
Do while loop Early BASICs (such as [[GW-BASIC]]) used the syntax WHILE/WEND. Modern BASICs such as [[PowerBASIC]] provide both WHILE/WEND and DO/LOOP structures, with syntax such as DO WHILE/LOOP, DO UNTIL/LOOP, DO/LOOP WHILE, DO/LOOP UNTIL, and DO/LOOP (without outer testing, but with a conditional EXIT LOOP somewhere inside the loop). Typical BASIC source code:
While loop The "while" construct consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is "true", the code within the block is executed. This repeats until the condition/expression becomes false. Because the "while" loop checks the condition/expression before the block is executed, the control structure is often also known as a pre-test loop. Compare this with the "do while" loop, which tests the condition/expression "after" the loop has executed.
While loop In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The "while" loop can be thought of as a repeating if statement.
While loop Using a macro system, implementing a "while" loop is a trivial exercise (commonly used to introduce macros):
Do while loop Compare this with the first example of the [[While loop#Racket|while loop]] example for Racket. Be aware that a named let can also take arguments.
Do while loop In this manner, the do ... while loop saves the initial "loop priming" with codice_1 on the line before the codice_2 loop.
Do while loop With Fortran 90 and later, the do-while loop is actually the same as the [[for loop]].
JavaScript syntax The syntax of the JavaScript for loop is as follows:
Do while loop Some languages may use a different naming convention for this type of loop. For example, the [[Pascal programming language|Pascal]] language has a ""repeat until"" loop, which continues to run "until" the control expression is true (and then terminates) — whereas a "while" loop runs "while" the control expression is true (and terminates once the expression becomes false).
While loop Contrary to other languages, in Smalltalk a "while" loop is not a language construct but defined in the class codice_2 as a method with one parameter, the body as a closure, using self as the condition.
Do while loop The "do while" construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is [[Truth|true]] the code within the block is executed again. This repeats until the condition becomes [[False (logic)|false]]. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the [[while loop]], which tests the condition before the code within the block is executed, the do-while loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again. This process is repeated as long as the expression evaluates to true. If the expression is false, the loop terminates and control transfers to the statement following the do-while loop. In other words, whereas a while loop sets the truth of a statement as a [[condition precedent]] for the code's execution, a do-while loop provides for the action's ongoing execution subject to defeasance by the condition's falsity, which falsity ("i.e.", the truth of the condition's negation) is set as a [[condition subsequent]].
Do while loop Python lacks a specific do while flow control construct. However, the equivalent may be constructed out of a while loop with a break.
Do while loop These example programs calculate the [[factorial]] of 5 using their respective languages' syntax for a do-while loop.
Foreach loop For unordered iteration over the keys in an Object, [[JavaScript]] features the codice_23 loop:
JavaScript JavaScript supports much of the structured programming syntax from C (e.g., if statements, while loops, switch statements, do while loops, etc.). One partial exception is scoping: JavaScript originally had only function scoping with var. ECMAScript 2015 added a let keyword for block scoping, meaning JavaScript now has both function and block scoping. Like C, JavaScript makes a distinction between expressions and statements. One syntactic difference from C is automatic semicolon insertion, which allows the semicolons that would normally terminate statements to be omitted.
Do while loop The [[PL/I]] DO statement subsumes the functions of the post-test loop ("do until"), the pre-test loop ("do while"), and the [[for loop]]. All functions can be included in a single statement. The example shows only the "do until" syntax.
While loop Note that it is possible, and in some cases desirable, for the condition to "always" evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.
While loop first checks whether x is less than 5, which it is, so then the {loop body} is entered, where the "printf" function is run and x is incremented by 1. After completing all the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.