|
| 1 | +// basic syntax of for loop |
| 2 | +// for (initialization; condition; increment/decrement) { |
| 3 | +// statement |
| 4 | +// } |
| 5 | +// Initialization means where the loop starts |
| 6 | +// Condition is used to check if the loop should continue |
| 7 | +// Increment/decrement is used to update the value of i |
| 8 | +// simple for loop that will print 0 to 100 numbers |
| 9 | +for (var i = 0; i < 100; i++) { |
| 10 | + console.log(i); |
| 11 | +} |
| 12 | +console.log("-----------------"); |
| 13 | +// simple for loop that will print I will not late in class anymore 100 times |
| 14 | +for (var i = 0; i < 100; i++) { |
| 15 | + console.log("I will not late in class anymore"); |
| 16 | +} |
| 17 | +console.log("-----------------"); |
| 18 | +// for loop that will print table of 5 |
| 19 | +for (var i = 1; i <= 10; i++) { |
| 20 | + var tableNumber = 5; |
| 21 | + console.log(tableNumber, "x", i, "=", tableNumber * i); // 5x1=5, ... 5x10=50 |
| 22 | +} |
| 23 | +// Array is used to store multiple values in a single variable |
| 24 | +var cars = ["BMW", "Volvo", "Saab", "Ford"]; |
| 25 | +var dataLength = cars.length; |
| 26 | +// Using for loop to iterate over an array and print the value of each element |
| 27 | +for (var i = 0; i < dataLength; i++) { |
| 28 | + console.log("dataLength", dataLength); |
| 29 | + console.log("My car is", cars[i]); |
| 30 | +} |
| 31 | +console.log(dataLength); |
0 commit comments