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.

2 Likes

Hello everyone,

I was doing the While Loop Demo today and found a small bug in the exercise.
Instead of generating the default executable a.out using: g++ enter_pin.cpp, I generated an executable with a chosen name: g++ -o main.exe enter_pin.cpp.
When doing this and executing, codecademy does not recognize you finished the exercise, and therefore does not allow you to continue to the next exercise.

/* The following comments explain why you have up to 5 tries,
even though it seems like there should only be 3.
Copy and paste into the code editor for markup*/

#include

int main() {
int pin = 0;
int tries = 0;
std::cout << “BANK OF CODECADEMY\n”;
std::cout << "Enter your PIN: ";
std::cin >> pin; //pinenters=1
while (pin != 1234 && tries <= 3) {
std::cout << "Enter your PIN: ";
std::cin >> pin;
tries++;
//pinenters=2, tries=1
//pinenters=3, tries=2
//pinenters=4, tries=3
//pinenters=5, tries=4 (this finally exits the loop)
}

//the code gets here if either (or both) of the following conditions is satisfied:
//case 1. pin is correct
//case 2. pinenters=5, tries=4

//the following code then ensures that it must be case 1 to print the message.
if (pin == 1234) {
std::cout << “PIN accepted!\n”;
std::cout << “You now have access.\n”;
}

//hope this helped to clarify any doubts! :slight_smile:
}

Hi,

I’ve compiled the unmodified enter_pin.cpp.
When runing a.out, if I enter a letter (eg. “a”) for the pin, code inside the while loop gets executed four times.
To test what is happening, I put a line inside while loop.
After std::cin >> pin; I’ve put this line:
std::cout << "Entered is: " << pin << std::endl;
Output of this line, i case that letter was input for the pin is value of the pin and this is 0.

Please, can you explain why is it 0?
Why the loop is executed, four time?

Thank you.

Need to copy/paste your code here to get help with such questions.

Hi!

The correct code :
#include

int main() {

int pin = 0;
int tries = 1;

std::cout << “BANK OF CODECADEMY\n”;

std::cout << "Enter your PIN: ";
std::cin >> pin;

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

std::cout << "Enter your PIN: ";
std::cin >> pin;
tries++;

}

if (pin == 1234) {

std::cout << "PIN accepted!\n";
std::cout << "You now have access.\n"; 

}

}

The mistake , the variable “tries” has been initailizaited by value “0” but should be by value 1 .