FAQ: Loops - Do...While Statements

Because your code is incorrect. The while is bound to the do block. It does not see the code that follows which is simply a statement that executes.

do {
    // something
} while (condition);
1 Like

Hi. I do not understand why, after running this code, the output is 6, instead of 5. If the stopping condition is (cupsAdded < cupsOfSugarNeeded) and the value of cupsOfSugarNeeded is 5, the output should not be 5?

let cupsOfSugarNeeded = 5;
let cupsAdded = 0;

do {
   cupsAdded += 1;
}

while (cupsAdded < cupsOfSugarNeeded) {
     cupsAdded += 1;
     console.log(cupsAdded)
}

// Output: 6

As far as I know, the loop would stop running when cupsOfSugarNeeded value reach “5”.
PS: I know this use of “do… while” loop is not the most adequate, but I am just trying to understand why this output.

Thank you! :slight_smile:

Sorry, i still don’t get why :
let cupsOfSugarNeeded=9;

let cupsAdded=6;
do{

cupsAdded++;
} while(cupsAdded < cupsOfSugarNeeded);

console.log(cupsAdded); // 9
and not 3 ? since cupsAdded = 6, if we add 9 cups, we have cupsAdded > cupsOfSugarNeeded.

We started with 6 cups, and counting up to 9 means we add 3 more. Since we are still adding to cupsAdded, it will increase to 9. We have no record of exactly how many are added in the loop but if we subtract what we started with from 9 we get 3. Still, we have the desired cups needed in the end, and that’s what counts.

1 Like

I’ve been thinking if it may happen because this last part of the code starts running after the “while” stopping condition evaluates to FALSE?
{ cupsAdded += 1; console.log(cupsAdded) }

I also noticed that we must not use const when we are declaring again the variable in the do… while loop.

For my case. i put const cupsAdded = 0;

however i called the cupsAdded again in do loop (cupsAdded++). this causes an error.

When i changed the cupsAdded into let. everything works fine.

1 Like

Yes, this happens because declaring a variable using const indicates that it is a constant. Therefore, its value should not change and when you try to do so, it will throw an error. We can use let to declare variables whose values do change.

2 Likes

Nevermind… I understand the structure is a comparison between while and do…while.
const firstMessage = ‘I will print!’;
const secondMessage = ‘I will not print!’;

// A do while with a stopping condition that evaluates to false
do {
console.log(firstMessage)
} while (true === false);

// A while loop with a stopping condition that evaluates to false
while (true === false){
console.log(secondMessage)
};
Would there be a real case in which we would want to use a paradoxical condition, such as true === false?

Short answer: No. The idea here was to see that at least one iteration occurred.

Hello. The output is 9 because it’s 9 cups of sugar added all together , 6 you started with (let cupsAdded=6) plus 3 more you added - one in each loop.

Accidently, I put console.log(cupsAdded) inside the do{ } , like this:

let cupsOfSugarNeeded = 9;

let cupsAdded = 6;

do {

cupsAdded++;

console.log(cupsAdded);

} while (cupsAdded < cupsOfSugarNeeded);

and for that reason my output on the console was:
7
8
9

It was confusing at first , but then I understood that because console.log is in the do part, the result after each loop was printed to the console. And that helped me to understand what was going on. So, we had three loops, each of them added 1 to cupsAdded initial value. When cupsAdded reached the value of 9, the loop stopped because 9 is not smaller than 9 :slight_smile:
If the value of cupsAdded is greater than cupsOfSugarNeeded the value of cupsAdded will always increase by one because do…while statement always has an initial loop that executes do before checking on the while condition.

I hope this is helpful, to someone , and since I am new to learning coding I hope that all I have written is correct.