I want this loop to decrement from the value I set it to from 10 to 0. Why does it keep going forever?
int lengthString = 10;
for (int j = lengthString; lengthString > 0; j--)
{
cout << j;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Change to
lengthStringdoesn’t change in your loop.Also, you might want to verify that
lengthStringis greater than 0 (in your real code) if you declarejas anint, because ifjis initialized to a negative number, the loop will run “forever” (until it overflows and gets decreased to 0 again, that is).