How should i set a while loop counter? When is it supposed to be 1 and when is it supposed to be 0?
In general, how should I start with a while loop problem?
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.
It depends on what you are doing and what you want to accomplish.
If you are iterating through an array, then you will probably want to start your counter with
0, since arrays are0-indexed (the first element of the array is at position0). For example:If you are not iterating through an array, it does not really matter what you start your counter with, but it probably does matter how many times you want the loop to iterate. If you want it to iterate 100 times, you could either start with
0and increment the counter by1untilcounter < 100, or you could start the counter at1and increment it by1untilcounter <= 100. It’s totally up to you. For example:Actually, for both of these cases,
forloops would probably serve you better, but the same concept applies: