Do while loop


#1


do while


I don't understand why when I run the code on a random webpage(HTML and linked the js file and used breakpoints) in the console it prints out twice the messege. I mean this is how I think it should work:
The program read's the value(true) it goes in to the "do while" function, it print's out the console.log, then the value changes to false and when checked in the condition if it is still true(which is not) it should stop but it dosen't, it leaves the do function, takes the value(true) and goes one more time so it print's twice the "console.log". My question is why the program leaves the do while function?Why dosen't it stop's when the value turn's false?
PS: If I run this code in your compiler, it only print's out once, so what's the difference?
PS: Even this code is printed out twice, it goes twice, why?
var youHit =Math.random()
console.log(youHit);

Replace this line with your code. 
var value = true;
do
{
console.log("What's up man");
value = false;
}while(value);


#2

This loop will never end called infinite loop.This will crash your browser.

value = "true"; // wrong

because here written like this true is a string.but true/ false Boolean data type. so write without quotes.

value = "false"; // same here


#3

still didan't anwered my question


#4

It is difficult for me to understand your question, but it console.log-s twice because

This loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

http://www.w3schools.com/jsref/jsref_dowhile.asp


#5

var youHit =Math.random()
console.log(youHit);

if this code, as well as the other one, prints twice. Then it has nothing to do with your code but with your setup. What kind of IDE do you use? Which browser do you use to display it. What do your HTML and JS file look like aso.


#6

I use Chrome and as a writeing program I use notepad++ if it's counts. No ideea what to tell you about IDE, and yes it print's out twice.


#7

IDE is an integrated development environment:

It's fine if you don't use one. Have you tried it in a different browser as well and does it work? Could you post the code that you're using and what do you use to see the output regular chrome web dev tools or special plugins?


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.