FAQ: Loops - The While Loop

Not only do the courses need to be re-reviewed for absurd and illegible use of English… but these forums require scrolling past 2 or 3 years of posts before being able to see anything current or post ?!

THIS PLACE IS A JOKE!

Hello!
I’ve been trying to understand the difference between for and while loops for a while but didn’t succeed.
I can’t see any difference between the two. I can see that we only breaks the syntax of the for loop to be a while loop.

In the course examples:

// A for loop that prints 1, 2, and 3
for (let counterOne = 1; counterOne < 4; counterOne++){
console.log(counterOne);
}

// A while loop that prints 1, 2, and 3
let counterTwo = 1;
while (counterTwo < 4) {
console.log(counterTwo);
counterTwo++;
}

Both have the same syntax but different arrangement.
Now I’ve read that we use the while loop, unlike the for loop, when we want a loop to execute an undetermined number of times. But even in the while loop we have to write a condition that determines when the loop should stop, therefore if we are telling the while loop to execute starting 0 (as a var) and while var < 1000, to add 1 to the var (var++) then we know that the number of repetitions will be 1000 loops.
Note: if we didn’t define a condition in the while loop we will have an infinite loop (a bad practice). So as long as we are defining a condition in the while loop, then we know the number of repetitions.
So what’s the difference between while and for loops

When there are a fixed number of iterations there is not much difference, since the while loop will have an incrementing (or decrementing) counter. In this case, a for loop might be a better choice.

Now, when there is an indefinite number of iterations, the while loop is the one to reach for, especially if there is no counting at all, only testing for a qualifying condition.

while (condition) {
    //  run zero or more iterations
    //  code
}

do {
    //  run at least one iteration
    //  code
} while (condition)

By indefinite we could also mean, infinite, such as a loop that doesn’t end until we end the session (stop the program).

while (true) {
    //  code
    if (condition) {
        break
    }
}

This is what makes it clear for me. Thank you so much!

1 Like

Hello guys,

I finished the instructions and the code seems fine(i have green ticks on all the instructions), but I have a question about the printed results. When i execute the script i am getting random results as i expected but at times i am getting more than 4 results, or lower than 4 results.
Can someone explain why ? Shouldn’t be always 4?

My code:

const cards = [‘diamond’, ‘spade’, ‘heart’, ‘club’];

// Write your code below

let currentCard;

while (currentCard != ‘spade’) {

currentCard = cards[Math.floor(Math.random() * 4)]

console.log(currentCard)

};

*** EDIT: regarding the results, as i though more about it, since the random number will be a single number, should the array only print one value only ?

Short answer, no. There is no way to predict what the random number will be. Since the solution set is so small (four numbers) it is not unlikely that a 1 will be returned after a small number of iterations, but it is also not unlikely that it could take more than four iterations.

1 Like

thank you for the answer, could you please see the edited part at the bottom ?
[
*** EDIT: regarding the results, as i though more about it, since the random number will be a single number, should the array only print one value only ?

]
Why i am getting more than one items returned from the array since the random number is a single one ?

It is inside a loop for which the breaking condition is ‘spade’ (index 1). The loop runs until that condition arises, which is not predictable, only inevitable.

1 Like

Okay, yeah this seems the logic behind it, thank you Roy :slight_smile:

1 Like

Thank you! Your explanation helped me process this!

Two questions regarding the code below:

  • Why do we not need to add an increment to this while loop? Is it because the variable has no value or because we do not need to increment in order to reach the stopping condition because the stopping condition is ‘spade’?

  • Why does it loop infinitely when I do add an increment? Is it searching for the variable or generating random numbers and incrementing them endlessly?

const cards = [‘diamond’, ‘spade’, ‘heart’, ‘club’];

// Write your code below

let currentCard;

while (currentCard !== ‘spade’) {

currentCard = cards[Math.floor(Math.random() * 4)];

console.log(currentCard);

}