Placing the condition with do/while loops? My 'do' is not DOING!


#1

In the following there are two programs. I put the "false" condition in the do statement.

 var getToDaChoppa = function(){
      do{ds = prompt("Do you have a car?")}
      while(ds === "yes")
        {ds = prompt("Tell the truth. Do you REALLY have a car?")};
        console.log("You really need to get yourself a ride bro")
      
    };
    
    getToDaChoppa();

What I would like to happen here is I want the program to ask If I own a car. If I say yes it will ask "You really need to get yourself a ride bro" over and over again unless I input anything other than yes. But what happens is it ask the prints "Tell the truth. Do you REALLY have a car?", the statement in the do block of code over and over unless I input "yes". I thought that code block was only supposed to run one time. Also I thought that we had to put the false condition before the while statement the way it is in exercise 7.9 But if I do this:

var getToDaChoppa = function(){
  x=5;
  do{console.log("This is a test")
  while(x>6)
    {console.log("This should not print")};
  console.log("This should print.");
  
};

getToDaChoppa();

I have tried moving the x=5 statement to different places but it never works. I clearly don't fully understand this simple thing :confused:


#2

We cannot combine a do..while with a while. It is one or the other.

do {
    // code statements
} while (condition);

OR

while (condition) {
    // code statements
}

There is no such thing as this...

do {
    // code statements
} while (condition) {
    // code statements
}

Study your code and correct accordingly, then see what you get.


#3

OOOOhhhh now I get it!! :relieved:


#4

In the case of a do..while we can define our control variable inside the loop body, so long as it is declared prior to the do statement.

var flag;
do {
    flag = Math.floor(Math.random() * 5 + 1) != 5;
} while (flag);

This loop will run until flag === false, which it will be when a 5 is generated. Notice where we defined flag? Prior to the do statement, if was undefined, but declared (and hoisted).


#5

A while loop statement requires that we define our control variable before the the while statement. In either case, we should never declare a variable inside a loop body.

function rand01(){
    return Math.floor(Math.random() * 2);
}
var flag = true;
var a, b, c;
while (flag) {
    a = rand01();
    b = rand01();
    c = rand01();
    console.log(a, b, a*b, c);
    flag = a * b === c;
}

In this construct we required flag to be defined else the loop would not run. Sooner or later, a * b is not going to equal c. That will redefine flag and end the loop.


#6

Here you declared flag before the loop but changed it's value in the loop right? Did you have to declare a, b, and c outside of the loop too?


#7

Better that they be declared but once, rather than repeatedly inside the loop. In the above, we declare them outside, and define them inside the loop.

The flag variable is both declared (var flag) and defined (flag = true) before the while statement.


#8

Why would they be declared repeatedly?


#9

Consider,

var flag = true;
while (flag) {
    var a = rand01();
    var b = rand01();
    var c = rand01();
    console.log(a, b, a*b, c);
    flag = a * b === c;
}

Here the variables are declared on every iteration of the loop. This is wasteful of resources and needless verbiage in our code. Plus, it will not go well for us when we present this code in our job interview.

To repeat, never declare variables in a loop body. Declare them beforehand. I'm saying this as a caution, not an admonishment, btw.


#10

Thank you very much!! You have been a great help!! :smiley: