With the while loop we can execute a set of statements as long as a condition is
For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. Show Loops are a way to repeat the same code multiple times. The for…of and for…in loops A small announcement for advanced readers. This article covers only basic loops: 6, 7 and 8.If you came to this article searching for other types of loops, here are the pointers:
Otherwise, please read on. The “while” loopThe 6 loop has the following syntax:
While the 0 is truthy, the 1 from the loop body is executed.For instance, the loop below outputs 2 while 3:
A single execution of the loop body is called an iteration. The loop in the example above makes three iterations. If 4 was missing from the example above, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and in server-side JavaScript, we can kill the process.Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by 6.For instance, a shorter way to write 6 is 7:
Curly braces are not required for a single-line body If the loop body has a single statement, we can omit the curly braces 8:
The “do…while” loopThe condition check can be moved below the loop body using the 7 syntax:
The loop will first execute the body, then check the condition, and, while it’s truthy, execute it again and again. For example:
This form of syntax should only be used when you want the body of the loop to execute at least once regardless of the condition being truthy. Usually, the other form is preferred: 0.The “for” loopThe 1 loop is more complex, but it’s also the most commonly used loop.It looks like this:
Let’s learn the meaning of these parts by example. The loop below runs 2 for 2 from 4 up to (but not including) 5:
Let’s examine the 1 statement part-by-part:partbegin 7Executes once upon entering the loop.condition 3Checked before every loop iteration. If false, the loop stops.body 2Runs again and again while the condition is truthy.step 4Executes after the body on each iteration.The general loop algorithm works like this:
That is, 1 executes once, and then it iterates: after each 0 test, 3 and 4 are executed.If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper. Here’s exactly what happens in our case:
Inline variable declaration Here, the “counter” variable 2 is declared right in the loop. This is called an “inline” variable declaration. Such variables are visible only inside the loop. 0Instead of defining a variable, we could use an existing one: 1Skipping partsAny part of 1 can be skipped.For example, we can omit 1 if we don’t need to do anything at the loop start.Like here: 2We can also remove the 4 part: 3This makes the loop identical to 9.We can actually remove everything, creating an infinite loop: 4Please note that the two 1 semicolons 1 must be present. Otherwise, there would be a syntax error.Breaking the loopNormally, a loop exits when its condition becomes falsy. But we can force the exit at any time using the special 2 directive.For example, the loop below asks the user for a series of numbers, “breaking” when no number is entered: 5The 2 directive is activated at the line 4 if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely, 5.The combination “infinite loop + 2 as needed” is great for situations when a loop’s condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body.Continue to the next iterationThe 7 directive is a “lighter version” of 2. It doesn’t stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).We can use it if we’re done with the current iteration and would like to move on to the next one. The loop below uses 7 to output only odd values: 6For even values of 2, the 7 directive stops executing the body and passes control to the next iteration of 1 (with the next number). So the 5 is only called for odd values.The A loop that shows odd values could look like this: 7From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an 5 block instead of using 7.But as a side effect, this created one more level of nesting (the 5 call inside the curly braces). If the code inside of 5 is longer than a few lines, that may decrease the overall readability.No Please note that syntax constructs that are not expressions cannot be used with the ternary operator 0. In particular, directives such as 9 aren’t allowed there.For example, if we take this code: 8…and rewrite it using a question mark: 9…it stops working: there’s a syntax error. This is just another reason not to use the question mark operator 0 instead of 5.Labels for break/continueSometimes we need to break out from multiple nested loops at once. For example, in the code below we loop over 2 and 5, prompting for the coordinates 6 from 7 to 8: 0We need a way to stop the process if the user cancels the input. The ordinary 2 after 0 would only break the inner loop. That’s not sufficient – labels, come to the rescue!A label is an identifier with a colon before a loop: 1The 1 statement in the loop below breaks out to the label: 2In the code above, 2 looks upwards for the label named 3 and breaks out of that loop.So the control goes straight from 4 to 5.We can also move the label onto a separate line: 3The 7 directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop.Labels do not allow to “jump” anywhere Labels do not allow us to jump into an arbitrary place in the code. For example, it is impossible to do this: 4A 2 directive must be inside a code block. Technically, any labelled code block will do, e.g.: 5…Although, 99.9% of the time 2 is used inside loops, as we’ve seen in the examples above.A 7 is only possible from inside a loop.SummaryWe covered 3 types of loops:
To make an “infinite” loop, usually the 03 construct is used. Such a loop, just like any other, can be stopped with the 2 directive.If we don’t want to do anything in the current iteration and would like to forward to the next one, we can use the 7 directive. 9 support labels before the loop. A label is the only way for 9 to escape a nested loop to go to an outer one.
Is a while loop a conditional statement?The While loop and the For loop are the two most common types of conditional loops in most programming languages.
Can we use or condition in while loop?As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.
What is the condition used in while loop?A while loop evaluates the condition. If the condition evaluates to True , the code inside the while loop is executed. condition is evaluated again. This process continues until the condition is False .
DoDo-While Loop is an exit-controlled loop that evaluates the condition after the execution of the body. Once the body is executed, then it checks the condition: If True, It will again execute the body of the Loop.
|