WHILE and DO WHILE loops

Description and different uses of the two types of WHILE loops that are available in Javascript, with some practical examples.

We are dealing with the different control structures that exist in the Javascript language and specifically looking at the different types of loops that we can implement in this programming language. In previous articles we already saw the first of the loops that we should know, the and now we are going to deal with the other two types of control structures to do repetitions. So, now let’s see the two types of WHILE loops that we can use in Javascript and the uses of each one.

WHILE loop

These loops are used when we want to repeat the execution of some statements an indefinite number of times, as long as a condition is met. It is easier to understand than the FOR loop, since it does not incorporate in the same line the initialization of the variables, their condition to continue executing and their updating. It only indicates, as we will see below, the condition that must be met for an iteration to be carried out.

while (condition){ //statements to execute }

A code example where this loop is used can be seen below.

var color = “” while (color != “red”){ color = prompt(“give me a color (write red to exit)”,””) }

This is an example of the simplest thing to do with a while loop. What it does is ask the user to enter a color and it does so repeatedly, as long as the entered color is not red. To execute a loop like this, we first have to initialize the variable that we are going to use in the iteration condition of the loop. With the variable initialized we can write the loop, which will check to execute that the variable color is different from “red”. In each iteration of the loop, the user is asked for a new color to update the color variable and the iteration ends, returning to the beginning of the loop, where we have to re-evaluate if what is in the color variable is “red” and so on as long as the text “red” has not been entered as a color.

See also  Why is it important to sign a contract?

Note: We have used the Javascript prompt function in this example, which we have not seen yet in this manual. This function is used to display a dialog box where the user must write a text. This function belongs to the Javascript window object and we discussed it in the article.

DO…WHILE loop

The do…while loop is the last of the loop structures available in Javascript and is a variation of the while loop seen above. It is generally used when we don’t know how many times the loop will be executed, just like the WHILE loop, with the difference that we know for sure that the loop will be executed at least once.

This type of loop was introduced in Javascript 1.2, so not all browsers support it, only version 4 or higher. In any case, any code you want to write with DO…WHILE can also be written using a WHILE loop, so in older browsers you’ll have to translate your DO…WHILE loop into a WHILE loop.

The syntax is as follows:

do { //loop statements } while (condition)

The loop is always executed once and at the end the condition is evaluated to say whether to execute the loop again or terminate its execution.

Let’s look at the example we wrote for a WHILE loop in this other type of loop.

var color do { color = prompt(“give me a color (write red to exit)”,””) } while (color != “red”)

This example works exactly the same as the previous one, except that we didn’t have to initialize the color variable before entering the loop. Requests a color as long as the entered color is other than “red”.

See also  Shape Tween

Example of using while loops

Let’s see below a more practical example of how to work with a WHILE loop. As it is very difficult to make practical examples with the little we know about Javascript, we are going to advance an instruction that we don’t know yet.

In this example we will declare a variable and initialize it to 0. Then we will add a random number from 1 to 100 to that variable until we add 1,000 or more, printing the value of the variable add after each operation. It will be necessary to use the WHILE loop because we do not know exactly the number of iterations that we will have to carry out (it will depend on the random values ​​that are obtained).

var sum = 0 while (sum < 1000){ sum += parseInt(Math.random() * 100) document.write (sum + "
“) }

We assume that as far as the WHILE loop is concerned, there will be no problems, but where there may be problems is in the statement used to take a random number. However, it is not necessary to explain the sentence here because we plan to do so later. Anyway, if you want, you can see this article that talks about .

Can .

With this we have already known all the types of loops that exist in Javascript, however we are still going to dedicate an article to explain those that serve us to alter the normal operation of loops in two directions.

Loading Facebook Comments ...
Loading Disqus Comments ...