FAQ: Loops - Do...While Statements

Tag me in a topic where you posted a question and we can give it a go.

They ask us to do this "We have a sweet tooth, so we want to add at least one cup of sugar to the batter even if the value of cupsOfSugarNeeded is 0 "
So, for me the code should be this :

let cupsOfSugarNeeded = 2;

let cupsAdded = 0;

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

// prints 3, so one more cup of sugar than the recipe.

Is it correct ?

1 is one more than zero, and 3 is one more than two, so it adds up.

1 Like

Thanks @mtf.
I known this was correct because I ran it.

But Codecademy gives this answer to this exercise :

// Write your code below
let cupsOfSugarNeeded = 2;
let cupsAdded = 0;

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

console.log(cupsAdded);
// 2

And this code doesn’t answer to what we have to do.

That is the only line needed. The difference between it and yours is that the loop runs three times whereas yours only runs once.

1 Like

I found this syntax to have worked for me as well:

let cupsOfSugarNeeded = 2;
let cupsAdded = 0;

do{
cupsAdded = cupsAdded +=1;
}while(cupsAdded < cupsOfSugarNeeded);

console.log(cupsAdded);

Нужна помощь. Тема заходит с трудом… Мой ответ отличается от ваших, но система признала, что ответ правильный. Почему?

Google Translate

Help is needed. The topic comes up with difficulty … My answer is different from yours, but the system recognized that the answer is correct. Why?

1 let cupsOfSugarNeeded = 1;
2 let cupsAdded = 0;
3 do {
4 cupsAdded = cupsOfSugarNeeded + 1;
5 cupsOfSugarNeeded++;
6 } while (cupsOfSugarNeeded < 2);
7 console.log(cupsAdded);

Nowhere in your code do you increment this variable. It must still be logging as, 0.

See the post just above my last post, above for a hint.

1 Like

Hi, could anyone help me pls,I ran this do…while loop and I was marked as pass,was it because a syntax of do… while was correct??thanks for reply

let cupsOfSugarNeeded = 6;
let cupsAdded = 0;

do {
cupsOfSugarNeeded = cupsOfSugarNeeded + cupsAdded; cupsAdded++;
} while (cupsAdded < 6);

console.log(cupsOfSugarNeeded);

// Output 21

why does not codeacademy accept this code as an correct alternative?
let cupsOfSugarNeeded = 1;
let cupsAdded = 0;
let i= 0;

do {

cupsAdded = cupsAdded + i;

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

There’s a problem stated in the exercise that confuses people:

The first task is to assign a number of our choosing to cupsOfSugarNeeded. Most likely everyone will choose a number that is not 0. This creates a conflict of basic concepts with the second task, where we want to add AT LEAST ONE cup. Doesn’t make much sense right? We want to add at least one, but the number needed is, let’s say, TEN. Why would the loop result to ONE?

let cupsOfSugarNeeded = 10;

let cupsAdded = 0;

do {

cupsAdded++;

} while (cupsAdded < cupsOfSugarNeeded);

console.log(cupsAdded);
// expected output 10

if we want the loop to produce the result the task asks us, by keeping the same value of 10 of cupsOfSugarNeeded, then we need to change the less than symbol < to greater than symbol >.

let cupsOfSugarNeeded = 10;

let cupsAdded = 0;

do {

cupsAdded++;

} while (cupsAdded > cupsOfSugarNeeded);

console.log(cupsAdded);
// expected output 1

why? because the while condition will result false and stop the loop. but the do will run at least once, and will increase by 1 the value of cupsAdded. There you go.

Alternatively just change the value of cupsOfSugarNeeded to 0 and keep the less than symbol < to your stopping condition.
In fact, step 2 implies that you need to change the value of cupsOfSugarNeeded to 0.

Hi,

I’m glad you mentioned the example! I don’t understand the use of true and false here and why it works. Your suggestion makes much more sense.

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)
};

The why is simple. In each case the condition is false. The first example logs because the condition is AFTER, where in the second does not since the loop body is never entered.

I understood the loop very well up until the “do…while” and I have passed this selection on “do… while” now but I am not happy as I still don’t quite understand how this is all linked up… :cold_sweat: I don’t feel that I have achieved something to pass this… :worried:

What is the minimum number of times a while loop can run?

What is the minimum number of times a do..while loop can run?

Correct, that’s how you would normally set up a for loop that works with user-created lists. In the previous lesson, we had to compare two separate arrays in order to find any matches, and a for loop (specifically a nested for loop) was the right tool for that particular job that explicitly needed our attention.

But in this case we’re working with a do…while loop, which only requires user-created conditions. It’s designed to handle automated tasks without supervision, meaning that we can literally “set it and forget it.” What we did here was load cupsOfSugarNeeded, set cupsAdded to 0, and pressed the start button. When cupsAdded increased one cup of sugar at a time until it equaled cupsOfSugarNeeded, the program stopped.