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 Introduction to Programming Control Structures Loops

Davide Pugliese
Davide Pugliese
4,091 Points

Java vs Javascript, how come this difference with numbering with cycling?

Hello, watching this video I noticed something that according to me deserves more explanations.

This has got something to do with states and the FSA theory. http://en.wikipedia.org/wiki/Finite-state_machine

An example of a FSA is the automatic machines that give chips, Coke cans, etc.

something = 10;

for (var i=something; something; something = something - 1) {

}

Now in this case when the interpreter hits 0, it gets out of the loop.

However if we write

something = 10;

for (var i=something; something >=0; something = something - 1) {

}

this changes completely the behavior, in fact in this case we have that the interpreter will go through the loop 11 times instead of 10.

Hello 1, Hello 2, ..., Hello 11

However, semantically I don't understand how 0 which is is a numeric value, can be both an integer as a well as a "false state".

In fact in other programming languages called strongly typed languages we would have to declare

var int = 10;

Can anyone clarify this concept, which maybe at a first reading is not important, but it's helpful to understand better the language and what happens under the cover?

Thank you

Britton Zirkle
Britton Zirkle
Courses Plus Student 15,005 Points

What you are asking is a comparison of logic vs. counting. Yes, zero is a numerical value as is one. When counting through a loop, if your final test includes some form of "=0", then the statement is both logically true and still has one more cycle until the condition is false and will terminate. In essence, you are testing if the current value is equal to zero. Since it is, the condition is true.
In an FSA or any other logic circuit, you are dealing with binary options. Either a condition has been met or it has not (1=true, 0=false). These binary conditions can be combined to form any number of conditions and complexities. The bottom line is that both zero and one are used to represent binary options as well as numerical counting. It is up to you as the person working with the code or system to be able to determine the context. In the case of your examples above, the first condition will not print zero because logically zero is not a value. Therefore, the last true condition will be something = 1. In the second example it will print zero because you have included something = 0 as a true condition. Neither of your examples will print the number 11 as you are starting at 10 and counting down.