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

how come the initial first time it knows to run counter = 10 then checks counter, then it proceeds to console.log?

how come the initial first time it knows to run counter = 10 then checks counter, then it proceeds to console.log("Hello World") and bypasses counter =counter- 1 ? However, the second time around it does evaluate counter=counter - 1 and comes up with a following solution which supersedes one before.

Ethan Lowry
Ethan Lowry
Courses Plus Student 7,323 Points

Hey, please post the specific code in question so we can get a better idea of what you're asking. Thanks.

Hi Paul,

Are you referring to the for loop or the while loop in the video?

3 Answers

The for statement is used for creating a loop.

The syntax of the for statement allows you to use 3 expressions in parentheses, separated by semi-colons:

for ( [initialization]; [condition]; [final expression] ) {
  // block of code
}

The first expression in the parentheses is used purely for initialization and is not part of the loop. The actual loop begins with the condition expression, and ends with the final expression.

If the condition expression evaluates to 'true', then the block of code enclosed in the curly braces is run. Then the final expression is evaluated and the current cycle of the loop is complete. The computer then returns to the condition expression and evaluates it again.

If the condition expression evaluates to 'false', then the loop is terminated and the block of code in the curly braces is not run, and the final expression is not evaluated.

So a while loop will continue to loop as long as what is in the brackets is true. True and false can be the boolean values of true or false, or it can be a binary number with 0 as false and 1 as true. In java script any integer above 1 is also true so when the loop starts at 10 (set by the variable 'counter') there is a true statement so it runs the loop. Every time the loop is ran the variable 'counter' decreases by 1 and runs over and over till counter gets to 0 and ends the loop.

One clarification to your answer - the first sentence should be revised to say "So a while loop will continue to loop as long as what is in the parentheses is true."

"While" looks to the condition within the "()" and then executes the action(s) within the "{}" once each time the condition within "()" is true.

Each time the loop runs the variable "counter" is decreased by 1, so the sequence looks like:

  1. "counter" starts at ten, which means the condition is true (any integer greater than zero is considered truthy) then at the very end of the loop the counter variable is reduced by 1 and returns to the "()" to evaluate whether it needs to run again.

  2. "counter" starts at 9, repeats the same behavior as the first loop, reduces by 1 and returns to the "()" to reevaluate.

  3. "counter" at 8 now etc.

  4. "counter" at 7 now etc.

  5. "counter" at 6 now etc.

  6. "counter" at 5 now etc.

  7. "counter" at 4 now etc.

  8. "counter" at 3 now etc.

  9. "counter" at 2 now etc.

  10. "counter" at 1 now and the actions within the "{}" are executed again, resulting in reducing "counter" to zero.

  11. When the condition "(counter)" is evaluated it is no longer true because "counter" now is equal to zero. There is no further action to take because the content within "{}" only run when the condition is true, but now "(counter)" would evaluate to false because "counter" is no longer greater than zero - it is zero and is therefore falsey.

If you have questions based on this response please reply directly to the comment.