FAQ: Loops - Do...While Statements

This community-built FAQ covers the “Do…While Statements” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Do…While Statements

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

hello,

In this example (look bellow) I understand the do…while` statement creates a loop that executes a specified statement until the test condition evaluates to false.
What I do not understand why you are using the variable countinString.
It is because you want to store the incrementation on variable i ?
Anyway why declare countString as a string ?

thanks , andre

let countString = ‘’;
let i = 0;

do {
countString = countString + i;
i++;
} while (i < 5);

console.log(countString);

3 Likes

I do not understand why cupsAdded stops at 3 when it is no longer less than cupsOfsugarNeeded when it at the same amount
let cupsOfSugarNeeded=2;
let cupsAdded=0;

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

cupsAdded=3

2 Likes

do…while will run at least once whether or not the condition evaluates to true.
why you made the // cupsAdded=3 // you define it again!! and you made two time increment ?

2 Likes

Consider…

do {
    // something
} while (condition);

It ends there. There is no such thing as,

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

It’s either,

do {
    // some action
} while (condition);

or,

while (condition) {
    // some action
}
19 Likes

Thanks for you great replay!
If we need to make a two time increment for this exercise, That’s mean I don’t understand the it!
I added one cup through (cupsAdded />/ cupsOfSugarNeeded) i make the condition false.
Onground I’m not understanding good and I start to see this topic in youtube.
Wish you a nice Day

1 Like

let cupsOfSugarNeeded=9;

let cupsAdded=6;
do{

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

console.log(cupsAdded);

why cups added output is 9 ? isnt suppose to be 3 ?

1 Like

Each iteration of the loop increases (increments by 1) the value up to 9, which is the termination condition.

I wonder why the programmer who invented this syntax used this:

do {
    // some action
} while (condition);

and not simply this

do {
    // some action
} 

That’s why it is confusing.

do {
    // some action
} while (condition);

Without the while is would not be a while loop, just a code block.

The only difference between while and do-while is when the condition is tested, before allowing the loop, which may not be allowed even one iteration, or after one iteration of the loop.

A while loop may run the code block at least zero times; a do-while may run it at least once.

10 Likes

I could not figure this one out.

after pressing the “stuck get a hint” button it suggested the following:

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

which I implemented as such:

const cupsOfSugarNeeded= 4;
const cupsAdded= 0;

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

console.log(cupsAdded);

This does put a checkmark in the excercise as if i had completed it, but throws a syntax error in the console

PS: I have tried with and without the ; after the ++

4 Likes

PPS: Figured it out. cupsAdded needs to be a let not a const.

14 Likes

Hi, did you get an answer to this? I am still confused why it starts at 0 and not 6.

The question sort of answers itself. Start with a value of 6, keep adding 1 until it reaches 9, at which point it will fail the condition test. A plain while loop would give the same result, and probably be a better choice of loop since the running variable is already defined.

@ jcinco, please post the code you are working on so we can see where the difficulty may be.

Hello mtf,
Please tell me if I am understanding this correctly.

Because the condition that has to be met, (in this case adding a cup of sugar: the “do statement”) happens before the code reads the “while statement,” which evaluates (as true or false), the condition will always occur one extra time over and above what the “while statement” would normally allow.

We would use DO…WHILE when we wish to have the code block execute at least once. This means we could define a variable within the code block, instead of before, and base the conditional on the state of that variable (or some other state) after running the code.

1 Like

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

Can someone tell me why the first message will print and the second will not? Is that because a do…while statement must run at least once?

That will never be true. The two booleans are diametrically opposite.

while (expression === true)

or more logically,

while (expression) {

}

I know, right? But this is example from codecademy course, not mine.

Consider,

var a;
while (a) {
  console.log(--a);
}

No matter what the inner code reads, it’s not going to matter. The loop can never run since a is undefined.

var a;
do {
    a = Math.random();
    console.log(a);
} while (a < 0.9);
0.8769109594930145
0.07216693941618191
0.8399564200147973
0.6291448345603665
0.7441578845207049
0.30244378134824546
0.9368155134116656

The last output will be the value that a refers to.

1 Like