Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Quinton Dobbs
Quinton Dobbs
5,149 Points

Why are there 10 numbers when it is 'counter < 10'?

Shouldn't 'counter < 10' come out with 9 numbers since 10 is not less than 10? Or does 0 actually count as a run through the loop?

Quinton Bolt
Quinton Bolt
1,071 Points

In programming, and Computer Science in general, we typically use a "zero-based" index. So when starting with 0, and counting while "counter < 10", you are going from 0 - 9, which is 10 numbers.

Here's a wiki page that can provide some more information on Zero-based numbering

Edit: Just noticed your name is also Quinton! Pretty cool coincidence!

Quinton Dobbs
Quinton Dobbs
5,149 Points

Awesome! Thanks for the information! Also, it's always fun to meet another Quinton. You can't help but feel like you just pulled a needle out of a haystack.

1 Answer

Hi Quinton,

You are correct in that the count will normally start at 0.

For example if we have a variable named "iteration" and it is set as 0 which will be used to record the number of times through the loop. Another variable named "lap" will be used to simulate a runner doing laps of an oval.

  • iteration 0: lap 1
  • iteration 1: lap 2
  • iteration 2: lap 3
  • iteration 3: lap 4
  • iteration 4: lap 5
  • iteration 5: lap 6
  • iteration 6: lap 7
  • iteration 7: lap 8
  • iteration 8: lap 9
  • iteration 9: lap 10

The iteration is 9 while the number of laps is 10.

Quinton Dobbs
Quinton Dobbs
5,149 Points

Great, appreciate the clear explanation.