Computer Science

Javascript While Loop

A JavaScript while loop is a control flow statement that allows a block of code to be executed repeatedly as long as a specified condition is true. It consists of a condition and a block of code. The condition is evaluated before each iteration, and if it is true, the code block is executed.

Written by Perlego with AI-assistance

4 Key excerpts on "Javascript While Loop"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • HTML, CSS & JavaScript in easy steps
    for loops are sometimes referred to as “pre-test” loops because their test condition is evaluated before any statements are executed.
    A while loop can be made to perform a specific number of iterations, like a for loop, by using a counter variable as the test condition and incrementing its value on each iteration. For example, a while loop structure to execute a set of statements 100 times might look like this:
    let i = 0
    while ( i < 100 )
    {
    statements-to-be-executed
    i++ ;
    }
    The counter variable is incremented on each iteration until its value reaches 100, upon which the evaluation returns false and the loop ends.
    Omitting a modifier from the while loop structure will create an infinite loop that will run forever.
    Create an HTML document with a self-invoking function that begins by initializing a loop counter variable
    let i = 10
    while.html
    Next, insert a while loop structure that will make iterations and output the value of the loop counter on each iteration until it reaches zero
    while( i > -1 ) { console.log( ‘ Countdown Number: ‘ + i ) i -- }
    Save the HTML document, then open it in your browser and launch the console to see the loop iterations
    Each while loop must have braces as they contain at least two statements – one statement to execute and a modifier. Do Loops
    Another kind of loop available in JavaScript is the do while loop structure. This is like an inverted version of the while loop, described here , and is ideal when the statements it will execute on each iteration absolutely must be executed at least one time. Its syntax looks like this:
    do
    {
    statements-to-be-executed
    modifier
    }
    while ( condition )
    The parentheses after the while keyword contain a condition that is evaluated for a boolean value after each iteration. Statements to be executed on each iteration are enclosed within braces along with a statement that modifies a value in the test condition, so that at some point its evaluation will return false and the loop will exit. While the evaluation remains true
  • Coding + Math
    eBook - ePub

    Coding + Math

    Strengthen K–5 Math Skills With Computer Science

    Figure 4.25 depicts the same loop program in four different potential learning contexts, each with a different level of detail and abstractness. While each approach differs in level of complexity, none appear to be beyond the reach of some students along the K–5 continuum of learning. Although students will differ on their readiness for one option over another, it is important that they are not limited by a lack of choices to pursue levels of complexity commensurate with their abilities.
    Figure 4.25 Comparison of While Loop program across different tools.
    RESOURCES
    RESOURCE SOURCE LINK  
    Flowgorithm Flowcharting Program Sacramento State University
    (flowgorithm.org )
    Online Flowchart Designer JGraph Ltd.
    (draw.io )
    SmartDraw (Online Flowchart Maker) SmartDraw Software, LLC
    (smartdraw.com/flowchart/flowchart-maker.htm )
    Coding: Loops Vocabulary Activity Flocabulary
    (flocabulary.com/lesson/coding-for-loops )
    Using While Lloops Khan Academy
    (http://bit.ly/3cMKD8z
  • Designing and Developing Robust Instructional Apps
    • Kenneth J. Luterbach(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)
    pointsEarned , after which the program will display the corresponding letter grade.

    Iterative Statements

    An iterative statement creates a loop, which enables the repeated execution of a set of statements. At times, programmers include an iterative statement within an iterative statement (or loop within a loop), which we will do after mastering straightforward loops.

    Repetition a Fixed Number of Times

    for (i = 1; i <= 7; i++) {// loop body      print(i); }
    The for loop above displays the numbers 1 through 7, each on a separate line. Initially, the loop control variable, i is set to 1. As long as i is less than or equal to 7, the statement or statements in the loop body are executed. Since braces {} surround the loop body, any number of statements can be included in the loop body.
    Run the for loop above in the JavaScript shell. Type the first line and hold down the Shift Key before pressing the Enter or Return key. In that way, the first line will not be executed yet. Then type the second line and again hold down the Shift Key before pressing the Enter or Return key. Lastly, enter the final closing brace} and press the Enter or Return key. Alternatively, you could enter the code into your text editor, then save and run.
    Once you have that loop working, editor and enter the following code in lates the sum of a set of numbers. Save open a new document in your text order to create a program that calcuthe file as nwnberCmnclierl.js.
    sum = 0; for (i = 1; i <= 5; i++) {      print ("Enter a number to include in the sum");      currentNumber = readline();      sum = sum + currentNumber; } print("The sum is " + sum);
    • Task 3.1 Enhance the numberCruncher1.js by also calculating and displaying the mean, which in this case is the sum divided by five (i.e., sum / 5 ).
    • Task 3.2 Always calculating the sum of five numbers is a severe limitation. Before the loop begins in numberCruncher1.js , prompt the user to enter the population size, N , which is the number of numbers to be summed. The variable you use for population size will replace the 5 in the for
  • Hack Audio
    eBook - ePub

    Hack Audio

    An Introduction to Computer Programming and Digital Signal Processing in MATLAB

    • Eric Tarr(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)
    end are repeated for the loop.
    When execution reaches the declaration of the loop, if the logical operation is TRUE, then the code in the loop is executed. When the commands within the loop are complete, execution returns to the logical expression at the loop declaration, and it is evaluated again. As long as the logical expression remains TRUE, the loop continues to repeat. At any point, if the logical expression produces a FALSE result, then execution jumps to the end of the loop.
    The following is a conventional approach for using a while loop. For an existing variable in code, the loop is declared using the while keyword followed by a logical expression containing the variable. Any commands to be repeated in a given task are executed within the loop. Also within the loop, the variable used in the original logical expression is changed in some way (e.g., iterative counting), such that the logical expression can eventually change from TRUE to FALSE, so the loop exits. Several examples of this approach are demonstrated in Example 5.11 .
    Examples: Save and run the following m-file in MATLAB.
    % This script demonstrates several examples of "while" loops.