FAQ: Loops - While Loop Demo

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

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

Learn C++

FAQs on the exercise While Loop Demo

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!

The first statements for asking the pin can be removed as they are stated in the exact way in the while loop. Makes for shorter code!

here in the while loop condition is given that try <= 3,means it will take upto 3 values . more than 3 values it will break. but after executing the program, it is taking upto 5 values . why?

5 Likes

I had this same question! I see this was posted in July 2019, and no response to it yet.

So I’ll give it a try, as my first response to any question on Codecademy.

Specifically, the question is: Why does the code on line 13 in the exercise:

while (pin != 1234 && tries <= 3)

continue to allow entries after three tries? Specifically, it allows five tries! Which is odd, because “tries <= 3” implies that the loop will stop after 3 tries.

What I see is two things going on here:

  1. The variable “tries” was initialised to 0 on line 6. This means that the computer starts counting from zero on line 17 (code: “tries++;”). So that gets us to four possible entries (unless you enter the correct pin code, of course).

  2. amonsoares above makes a good point, and this is part of the solution to this question. It’s unclear why lines 10 and 11 are included in the solution code. The two lines call for the pin and allow it to be entered with std::cout and std::cin, but they don’t increment the variable “tries”. So this code runs once. The program then runs through the “while” loop beginning on line 13, which allows the code to be run an additional four times (see point number 1 just above).

  3. Solution: To clean up the code and make it run (more or less) correctly, you have to do two things:

A. Delete lines 10 and 11, as they are completely redundant.
B. On line 6, set “tries” to 1 instead of 0: “int tries = 1;” OR B. On line 13 in the while loop, write the end of the parenthetical as “&& tries < 3”.

Just as a note, I’ve gone through the enter Loops section and at no point is this explained as an example of a coding error. It would, however, make an excellent addition to the section on Bugs (as a logical bug not detectable by the compiler).

Hope this was helpful! Have a good coding day, everyone.

1 Like