8. when to while and when to for


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/2/1?curriculum_id=506324b3a7dffd00020bf661#


Im being told i have a syntax error. If you guys could help me find it, that'd be dope.


var ben = true

while (ben){
    console.log("Ben is the coolest");
    ben = false
};

For (i = 0; i > 3 ; i++){

    console.log("Your Damn Right!!!");
 }


#2

should be,

for (i = 0; i > 3 ; i++){

Never capitalised.


#3

Well your FOR cycle is never going to start.

Firstly you have to declare variable i before its initialization, then you are assigning zero to i, but in condition you say "do the cycle if i is greater than 3", but since your i is zero, the cycle will never run.

To answer your question:
FOR cycle is used when we know how many items we will run through in the cycle (number of items in inventory, number of students in class etc...).
WHILE cycle is used when we don't know how many items we will run through in the cycle (while true in some list etc...). Also don't forget to increment both cycles, otherwise you end up in infinite cycle :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.