While loop. Shouldn't this work the opposite?


#1

I feel like I'm going crazy here. This is the code I wrote:

var condition = true;
while (condition === true){
console.log ("works!");
var condition=false;
}

This crashes.

So just messing around, I tried making the var condition = false at the beginning instead. It outputs "works!" Shouldn't it be the opposite? I don't understand.


#2

One error I see is that you are initializing another "condition" variable inside the while loop, because you used the "var" keyword. In fact, since the identifier of the second variable is the same as the first, name clashes may exist. Try changing "var condition = false" to "condition = false" and see what happens.

Also, you don't need the "=== true" part, because the variable should return the value of true or false.


#3

What @chibi_matoran said :slightly_smiling:
But what do you mean by crashes? What is written there is no infinite loop and should not crash. var condition = false at the very beginning "nothing" should happen meaning that because the condition of the while loops is false this part would be just skipped. I copy&pasted your code to reproduce the errors but it actually works as expected. Have you tried refreshing the page or could you make a screenshot?


#4

Hmm the plot thickens. Please bear with me, I've only been doing this a few hours. Chibi I tried removing the var but the same result occurred. Hax, I tried putting the code by itself and it worked, just like you said. It must be the rest of the lesson I was working on that was causing the problem.

After a bit of a headache, I've isolated the issue. (this is a lesson where you write a for, do, and while loop at once). The code below works just fine.

var condition = true;
while (condition){
console.log ("works!");
condition=false;
}

BUT, if I add in the "do", it falls apart and the browser freezes or crashes, as pictured below. If I delete the "var condition" 1st line, it spits an error saying the condition isn't defined. But whenever I define it (no matter what I define it as, and despite weather or not I use var) it crashes.

var condition = true;
do {console.log ("done");
}
while (condition){
console.log ("works!");
condition=false;
}

It's just a silly tiny lesson. But I'm really trying to wrap my head around this stuff to learn it properly.


#5

Not sure if this has been solved yet, as I didn't read all the way down. Mine would lock up as well...the problem that I found is that "condition" might be a reserved word in the language. I changed the variable name to curCondition and it worked fine.
Here's my code

var soloLoop = function()
{
  var curCondition = true;
  while( curCondition === true)
  {
      console.log (" Looped once! ");
      curCondition = false;
  }
  
  
};

soloLoop();

BTW, I absolutely HATE how JS and Java editors always set the curly braces out of line. I always change it to how I did above, as it's easier to debug when your adjoining curly braces are aligned vertically.


#6

Also, why people care so much about:

while(curCondition === true)

or

while(curCondition !true)

?
has always bugged me...if you think it's bad code go reply to stuff on devshed or something...this is entry level on here.
The compiler still knows what's going on.


#8

The do-while loop and the while-loop in your program are being mixed. Do-while and while are separate control structures and cannot be combined.

while(booleanCondition) {
function();
}

do {
function();
} while(booleanCondition); //Please note this semicolon!

Your program uses the same "while(condition)" for both the do-while and while loops. You need to use two separate "while"s for each individual loop, and cannot combine them.


#9

Simply using (condition) makes the code more concise. Habits start early on, so beginning programmers should be taught good practices early on.


#10

For me condition works fine although it is a pretty lame name for a condition but that depends on personal preferences (condition is rather a place holder name for instructions).

while(curCondition !true)

This would not work.

And the other one.

while(curCondition === true)

vs

while(curCondition)

Well for boolean inputs its easy to show that they do the same so the shorter one is usually preferred.


#11

Yes, "Condition" doesn't seem to be an issue, but it was a good thought. I had a similar one earlier and tried changing it to potato.

Ahh okay it makes more sense to me now. I thought "do" was something you just added onto the existing regular while structure. But it's a separate entity. Adding a
{
function();
}

to the end of my do-while is the whacky bit. I feel like I understand a little better now (hopefully!). Thanks everyone.


#12

You're absolutely right. I will say though, at the "beginning" phase of learning programming, slamming them for making their programs do what they want them to will deter them altogether. My C# professor told us "There are a million and a half different ways to do the same thing in programming. " , and he's absolutely right. I think "mentors" should beat commenting into beginning programmer's heads more than how they got something to work. What's more frustrating, someone arriving at an answer in a less "concise" manner with no comments about what they were thinking in the code, or code that only a few people can easily read and see what's going on. I tutor beginning C++ and I encourage my students to number comment their curly braces so they don't get lost, but I have yet to see a professor take off points for arriving at a solution in a different manner. Also I will add, codeacademy's scripting editor and compiler/checker lets A LOT of incorrect stuff through, which also teaches some bad habits. Just food for thought.