FAQ: Loops - While Loops

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

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

Learn How to Code

FAQs on the exercise While Loops

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!

I had changed the loop of pink-orange-mint to try something else. Instead, I did pink-orange-mint-purple, which is 4 colors total. My condition was “loop while under 15 tiles are placed”. After running it, I realised that the loop stops at the 16th title, which is against the condition I chose. I understand that it is because my color loop is complete at the 16th title, but shouldn’t the computer stop at 15th title to complete the condition? What is the reason it can’t stop?

Welcome, @ekina!

What this has to do with is the number of instructions within you loop body. Since there are four instructions, four tiles get placed each time through the loop. All the tiles get placed. When the program discovers 15 or more tiles it exits the loop. Try adding another color (5), or go back to 3 and you will see it places 15. Go up to six colors and it will place 18.

Ultimately, the loop ends when the condition is no longer met, not necessary at 15, but any value 15 or greater.

3 Likes

So my question is how do i create a loop solo, without a precursor or operator to initiate the loop. I enjoy the task of manual labor but is there any way to personally create a loop without the plug? I am only on the 3rd slide so i am unaware if there is soon to be an introduction to create our own loop effects by manual coding. Sure there is the tedious literal retyping of a function but is creating a loop an ability of koders?

Loops are an integral part of programs. Along with functions, they let programmers use a single statement over and over again without repeatedly typing it.

A while loop has a condition or state that must be met for the loop to continue. Once the condition is not met, the loop terminates.

There are no restrictions on what condition we use, so long as it is pertinent to the program. It might be a numeric value that is either growing to a maximum, or shrinking to a minimum. That variable would need to be declared before the loop, and modified within the loop.

Another case might be where an object is either growing in size (length) or shrinking to nothing. When you get to break we could discuss a static state in the while signature and a conditional within the loop. We’ll save that for later.

thanks, you for sure are a comfort of knowledge to rely upon. Onward to BREAK.

1 Like

I’m just curious, what happened when we accidentally created an infinite loop? Will the computer throw an error? Will there be any certain warning? How to stop it if it’s already running?

In many cases the interpreter will eventually throw an error, but if it doesn’t then refresh the browser (if it’s JavaScript running) or in the case of Python, and perhaps other languages, Ctrl+C will perform a keyboard interrupt.

1 Like

Ah I see.

Will there be notification that specifically stated that it’s an infinite loop error, or perhaps just a generic error?

If there is an exception or error thrown then it will likely have a fairly explicit message. One would hope it declares the run-on.

Some infinite loops occur when recursion is not adequately bounded. That may be another topic, but the two are related but for one difference… Recursion is based on an infinite loop principle but expected to have one or more cases that cause the stack to wind down. The stack is full of calls made to this function that depend upon the result of the later call. Once we wind it down, we hand the result backwards up the stack until the final call is returned. As I said, it is another topic.

while (true) {

}

With no breaking condition, this loop will run forever. An alternative to a breaking condition, or rather in addition to one, we can have the user providing input at each iteration. A lot of games or constantly alert programs use this principal. The key concern is that the USER dictates when the program ends.

1 Like

This is a very clear explanation! Makes me want to learn more :smile: Thank you @mtf!

Yes, this totally makes sense to me.

Thanks again!

1 Like

You’re welcome, @cyelz!

You go!

1 Like