Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.