Looping with While and Do While Loop


In this article, we will take a brief look at while and do while loop. Well, Sometimes there is a certain situation where you need to execute some task repeatedly. In that situation, we have a great solution while and do while loop.

What is a Loop?

Let me first describe what is looping means. Looping is a sequence of instructions that is continuously repeated until the certain condition is met. In simple words, looping is a continuous task that happens until the condition is true. when the condition has become false then the looping terminated.

The JavaScript has 4 looping statements.

  1. while
  2. do/while
  3. for
  4. for/in

In this article, we will discuss while and do while loop. One common use of loop is iterate over the element in the array. We will discuss array and advanced looping in the future tutorials.

While Loop

While Loop

Syntax:-

So the first JavaScript will evaluate the expression. If the value of the expression is true then it will print the statement until the value of the expression has become false. On the other hand if expression return false then JavaScript skip the statement and execute the rest of the code. In simple word,s while loop only executed when the expression returns true if the value of expression return false then the while loop terminated.

To wrap multiple statements in the while loop you can use compound statement using open and closing curly braces. So let’s take an example and understand how while loop works.

Example

So in this example, we print 0 to 9 number on the console.

As you can see we created a variable count and specify value 0 to that variable. Then we created while loop and in the expression, we specified count value is less than 10 then we wanted to print the count value on the console. To print count value where using the console.log method. And to increase count value by one we used count++ shorthand operator. We will take a brief look at operators in subsequent lectures. But just for now to increase count value buy one we use ( ++ ) sign.

You can do the same thing using this code so we will just say

Count = count + 1; // both statements are equal

Both statements are equal. but choosing a minimum code will always help you to maximize your performance. So when you execute this statement you will get 0 to 9 numbers on the console. But how did this happened? So the expression will first check the count is less than 10 if it is true then we print count value on the console so the JavaScript print 0 on the console because 0 is less than 10.

Then the next statement will increase the value of the count variable by one and now the loop will go on the top again. Then it will check the expression again with the new variable value so it will check one is less than 10 or not and if expression return true then it will print the value on the console and again increase the value of count variable by one.  so the JavaScript will do the same thing over and over and print 0 to 9 number on the console.

But how did the loop terminated. When the count variable value is equal to 10 then the expression become false because 10 is not less than 10. So the expression returns false and skip the compound statements and finally terminate the loop. You can put multiple statement and complex expressions in the while loop. its upon you to operate while loop.

Do-While Loop

Do while loop is just like a while loop but except that the loop expression is tested at the bottom of the loop rather than at the top. We first declared do section and then specify While with expression. It means that the body of the loop is always executed at least once. Using do while loop we first execute the statements and then evaluate the expression. JavaScript run the program from top to bottom that is why it will first execute this statement and then evaluate the expression. So following is the syntax of doing while loop.

Do While loop

Syntax:-

Don’t forget to put the semicolon after the expression parenthesis. You can put multiple statements in the do block with compound statements using curly braces. Occasionally you have a certain situation where you need to execute low at least once at that time do while loop is very helpful.

Example

So let’s take an example of a do while loop.

Now in this example, We are printing count value on the console using the console.log method. Using concatenation we concatenate the string and count value together And after that, we increase the count value by 1 using + + shorthand operator. and then after the compound statement closed. We will specify while with the expression in this expression we will check count value is less than 5 or not.

So now let me first execute this code and show you the output. On the console, you will get count value from 0 to 4. So the do while loop first executes the do block and then move to the while expression. Then the do while loop evaluates the expression and if he gets true value then the execution will move at the top and execute do block again. so this will do the same thing again and again till the value of the expression will become false. when the expression return false then the looping terminated.

So that is why when the value of the count variable becomes 5 the expression will check that 5 is less than 5 or not.  as you know 5 is not less than 5. 5 is equal to 5. so the expression returns false. And the loop will terminate. Now you will think this is the same as while loop. isn’t it. It is not. do while loop work like while loop But there is one difference in do while loop. As I said to do while loop executes at least once.

So if I change this expression and check if the count value is less than zero then What would be the output. let’s see. When I execute this code you will get count value 0 on the console. So do while loop executes 2 block 1st and evaluate an expression. so in the expression, we specified the false condition. So the expression will returns false and terminate the loop.

As simple as that. That’s it.

That is all for this article. We will see you in the next article. Our while and do while are explained. In the next articl,e we will move forward and learn for loop and nested for loop.