5 + 5 = 11: Looping from Zero

In this lesson, he uses a for loop with it’s iterator starting at 1 and going to the target number, inclusive. Or,

for (int i = 1; i <= MaxGuesses; i++)

This is fine, and I understand that for total beginners, it may be more intuitive, but try to structure your for loops with i = 0 and using i < TargetNumber instead.

Why?

Well, there’s no difference, except that starting from 0 is standard in all programming. Even if it’s not your own personal preference or “personal standard”, you will encounter it so extremely frequently in programming in all languages that it’s one of those things you just have to learn and get used to and adapt to, or you’ll keep getting confused by it.

Typically, I’d say ditch what everyone else is doing and do what logically makes sense. But in this case, if you try to stick to initializing your iterators at 1, you’ll be overrun by the people (and computers!) that count from 0 (zero).

When a baby is born, are they 1 year old? No! They are 0. They become “1 year old” after living for 1 entire year. So if you’re born in 1985, you are “1” in 1986, right?

Here’s an old dad joke – 5 + 5 is 10 right? Okay, now count with me on your 10 fingers…
With your left hand, count
1
2
3
4
5

Now on your right hand, count
10
9
8
7
6

Okay - what’s 5 + 6?
11.

5 plus 5 equals eleven!

No, silly, because you count from zero! So when you count on your right hand, you must start at TotalNumber - 1. Or 9. In for loops, this simply means we count to less than rather than less than or equal to. So,
i < targetnumber instead of i <= targetnumber, and we start i at zero instead of 1.

This is how you will find for loops in the wild.

for (i = 0; i < TargetNumber; i++) { }

Shed the training wheels when you’re ready, but shed them sooner, rather than later.

3 Likes

I think the inclusion of the dad joke is a good metaphor. I believe he does revisit this later in the course, but you are right, that there is no reason it can’t be adopted now.

This is a great explanation. I think more people should raed this. I will have my little brother raed this tonight, after I get home school, if my dad lets me.

This is very important and I know that @GavinMilroy is trying to teach best practice and I would agree that starting at 0 and looping until < is what most people would do.

Maybe Gavin will be able to rework the video for us.

Might be that you need to shed your training wheels and deal with starting at 1 :smile:

Seriously, if you’re given something that starts at 1 because there’s no point to having 0, then will you still start at 0 and do a useless loop? Sounds like you would.

And no one says, “My baby is 0 years old”. It would be ie “1 month old”.

So in that case you can skip 0 as it would be useless. If you just don’t want to include 0 in the loop, variables, maps, or whatever I see no problem with that as its not used. What would be used is switching from years to months or even having “Not yet born” selection which makes far more sense than 0 - although in this case, the back end you could do a 0 for not yet born should you have that as an option but it could be -1 for no application as well.

Of course, you can also start at 0 and do a + 1 which is also extra work.

And of course, everyone does things that cause someone to do what you do just as others will do the same to you.

adult-children_985

http://www.stbeals.com/success/s3jr5s6mnswzehj3efy2hwgsw83r26

Ok, but in this example, you’re counting years, not months. When you’re born, you’re not suddenly 1 year old, you’re 0 years old. Whatever number you have is the end point, not the beginning or some point in between.

Let’s take tanks for example (…since that’s relevant in the course). If you fire from the turret, the projectile is zero meters away from the turret when it starts moving. The projectile wouldn’t just magically appear 1 meter away from the tank, and then start flying at the enemy.

Privacy & Terms