While Loop and Do While Loop Work

While and do-while loops are very similar they both allow you to repeat code the while loop.

While Loop

 var n =0;

while (n < 5) {

n++;

console.log(“n =” +n);

}

var i = 5;

 

I have an example of that right here the while loop creates a loop that executes a specified statement as long. If the test condition evaluates to true and the condition is evaluated before executing the statement. So while it is less than 5 what wallace is true it’s run all these statements in the curly braces. And in the statements it’s going to add 1 to n and it’s going to cancel that log in equals n. The important thing to know about while and do-while loops is that it’s not automatically going to increment your counter. Like what happens in a for loop so you have to make sure you do that right.

Do While Loop

do {

  i++;

  console.log(“i =” +i);

} while (i < 5);

 

Inside the loop the dohile loop is extremely similar except. This is going to create a loop that the execute until this test condition evaluates to false. So the condition is evaluated after executing the statement resulting in this these statements these. Are going to run at least one time, it’s going to do this stuff . While I is less than 5 and then when I not less than 5 anymore it will stop.

 

 

So one of the key differences is that this is always going to run at least once it’s always gonna do this whole thing here and then see if I is less than 5 and then if that is we’ll stop here it’s going to check if n is less than 5 at the beginning and if it’s going to run this code in here so let me run it now so you can see what happens so you can see N equals

 

That looks exactly the same but now if I change this in to 5 so it’s going to start at 5 it’s gonna be while in is less than 5 and it’s already not less than 5.  So it’s not going to run this but here it’s going to run this whole thing then it’s going to check if I is less than five and it’s going to stop. Let’s see it just says I equals six which is we would even have I equals six last time but remember I is gonna start at five it’s going to increment. Up one and they want to consult it’s it’s six and I’m going to stop because I is not less than five. While I is less than five well I is not less than five it’s gonna stop so let’s change this back to 0 and I’ll just show you.

 

A few more quick things you can also use break and continue. If you use continue if n=3 okay we have if n=3 then continue the continued command means to not run any code after that command. Just go to the next iteration of the loop so if you have continued it’s going to skip anything below. The continue is gonna skip this it’s just go back to the top and do another check. if n is less than 5 and then it’s going to increment in again so let’s see that okay. Look right here

n=1

n=2

and does not equal 3

n=4

n=5

because it’s skipped n equals 3.

 

If you use break here break is going to just jump out of the loop completely. So it’s not gonna run any more iterations of the loop. Let’s see that okay right here with n=1, n=2 we don’t have any query out of the loop. If you accidentally make a loop that goes forever and then your code and then you run the code your browser could freeze up.

This is called an infinite loop and it could crash your browser if you’re using Chrome the solution is to click window in the top bar and then go to the task manager. And then in the process and other browsers have a similar thing and then if you’re using code pin like I’m doing the default setting.

 

Is that the JavaScript runs automatically as soon as you go to the page and this could cause your browser to freeze up again if it runs the same info loop again. So if you know your code as an infinite loop add the following to the end of the web address bar the question mark turn underscore underscore JX equals true.

And then if you add that to the end of the address bar it’s not going to run automatically you may have to instead of putting a question mark here you may have to put in and sign. if you already have a question mark in your address bar well that’s while and do-while loops.

1 Comment

Add a Comment
  1. “while (n 6 not stop.

Leave a Reply

Your email address will not be published. Required fields are marked *


TechinfoHQ © 2019