Easy code: But still, not giving me the result I want (do/while)


#1

Hey guys, when run this it gives me 1 "game on" but i want 4 but it doesn't work that way why?

Any help is highly appreciated.

var wire = 1

var getToDaChoppa = function(){
  do {
      alert("We are doing this once!");
  }
  while(wire > 5 ){
    console.log("game on");
    wire++
  }
};

getToDaChoppa();


#2

This post was flagged by the community and is temporarily hidden.


#3

It's incrementing your wire variable only once, because while condition is always false. I think it's working like you would paste code from while block into do block.

var getToDaChoppa = function(){
  do {
    alert("We are doing this once!");
    console.log("game on");
    wire++
  }
  while(wire > 5);

do { ... } while(...) syntax looks like:

do {
    code block to be executed
}
while (condition);

#4

Oh okay! So whenever I write while() it will always be false?

I thought you have to mention the condition before hand i.e. declare the variable before e.g.

var understand = 1; // true, declared

while(understand){ //start
console.log("I'm learning while loops!");
understand = 0; //false, end
}


#5

I meant that in your previous loop your condition (wire > 5) was false on start of a loop, so it was executed only once.

var understand = 1; // true, declared

while (understand) { //start
    console.log("I'm learning while loops!");
    understand = 0; //false, end
}

This loop will execute only once either.

The difference between do... while and while loops is that do... while is always executed at least one time, because do block is executed before checking condition in while.


#6

Hello again,

I tried to understand what you said but here's my confusion, sorry for my beginner understanding of this matter, how is (wire > 5) set on false? On top I wrote var wire = 1, which is true right?

I want to basically that the computer does "do" on the alert("We are doing this once"); then proceeds to do the while loop 4 times i.e. incrementing by one each (wire++).


indent preformatted text by 4 spaces
var wire = 1

var getToDaChoppa = function(){
do {
alert("We are doing this once!");
}
while(wire > 5){
console.log("game on");
wire++
}
};

getToDaChoppa();

indent preformatted text by 4 spaces


#7

You didn't understand how do while loop works. It doesn't execute code in do and then proceed to while loop. It's flow looks like:
1. Execute code in do block
2. Check condition in while
2.1. If condition is true, go to point 1.
2.2. If not, end loop.

Your condition is set to false at start, because wire = 1 and when loop is checking condition, 1 > 5 is false, that's why loop ends.


#8

I'm surprised that by this point nobody has noticed what will happen if wire ever does become greater than 5.

There is great danger in do {} while (); since the code gets to run at least once before testing the state against the condition. With while () {} the condtion must be met in order to run the code.

When writing a loop (a control flow construct) it's advisable to begin with one that cannot fail.

do {
    break;
} while (true);

The condition would create an infinte loop, were it even seen. Thankfully, break broke the flow. We have a program that stops itself. Not practical but worth looking into. We have an infinite loop if we remove the break

Not something to toss aside, mind. Think if your digital clock didn't run on an infinite loop? Or your computer itself, always listening (infinite loops with built-in interrupts). We need infinite loops, just not in our programs except on the outermost shell, and we need to escape this at the user's whim.

var user_input;
do {
    user_input = prompt("Eh?");
    if (user_input === null) break;
    console.log(user_input);
}
while (true);
console.log("Bye")

The extent of a loop such as this is unbounded, so long as it is the outermost shell of the program and has a direct route to the exit, namely, back to the command line, with a response as applies.

We could put our entire game inside this shell (as a function call, of course). The line, console.log(user); is just a fill-in. An entire program can be executed here.

Okay, back to the beginning... What is it that we MUST do at least once? Well, the above code sample is an adequate example. Why have a prompt() outside of a loop only to have the same prompt() inside the loop? do..while solves this and removes the redundant statement.

First we get input, already iniside the loop, then we test the input. We can run all manner of tests on the input, but the most important one is the Cancel button and the Esc key. That's why we test for null. That's the user's signal they want out. We better let them since it's only fair (and good programming).

So in this instance we let the user input control the loop. Only Cancel or Esc will ever cause the loop to end.

Now when we look at this lesson, it is highly contrived and not practical in the slightest, but I think that's what the author intended. To draw attention to what is happening in this construct. My post is not about solving the lesson, just reinforcing it with some loop theory, and some safety tips.

The above sample also illustrates that apart from declaring a variable so we don't use var inside the loop (an avoidable practice), it does not need to be defined upon entering the loop. This is an important point to note with NB in your notes.

For a while() loop to asses a variable, it needs to be defined before the loop. In the case of do it can be defined inside the loop, just as we did above with user_input.

On the whole, it is the above consideration that will determine if do is the right approach. It's not a decision to be taken lightly, and not an either-or type of question. The real question is 'should I use do?'


#9

Thank you for your afford to explain to me the details of this code! Much appreciated.


#10

Thanks again, I think that is what I got wrong - the fundamental of what the program intends to do..