|
| 1 | +// NOTE - FoR LOOP |
| 2 | + |
| 3 | +/******************* |
| 4 | + What is loop ? Loop is control structure of programming, it allows us to perform iteration, iteration is just a way of repeating must certain stuffs over and over again until a certain condition is met. |
| 5 | +*******************/ |
| 6 | + |
| 7 | +// NOTE - syntax |
| 8 | + |
| 9 | +// for ([initialExpressions]; [conditionExpression], [increment]) |
| 10 | + |
| 11 | +// E.g - just loop between numbers from 0 - 10 |
| 12 | + |
| 13 | +for (let i = 2; i <= 10; i += 5) { |
| 14 | + console.log("number" + i); |
| 15 | +} |
| 16 | + |
| 17 | +// NOTE BLOCK SCOPE |
| 18 | +/* |
| 19 | +------------------------------------------------ |
| 20 | +loop are also considered as block, just like if/else statements, any variables we define using let && const wont be accessible outside the scole |
| 21 | +
|
| 22 | +------------------------------------------------ |
| 23 | +*/ |
| 24 | + |
| 25 | +for (let i = 0; i < 100; i += 10) { |
| 26 | + const msg = "Number" + i; |
| 27 | + console.log(msg); |
| 28 | +} |
| 29 | + |
| 30 | +// if i try to log `msg` here, its not accessible i would a reference error |
| 31 | +// console.log(msg); |
| 32 | + |
| 33 | +// note - the code below will check in the loop when it meet the condition in the if block and log/display what is in there |
| 34 | + |
| 35 | +for (let i = 0; i <= 10; i++) { |
| 36 | + if (i === 7) { |
| 37 | + console.log("7 is my favorite number"); |
| 38 | + } |
| 39 | + |
| 40 | + console.log("Number " + i); |
| 41 | +} |
| 42 | + |
| 43 | +/* |
| 44 | +# NESTED LOOOPs - at some points, its possible to nest two or loop in each other |
| 45 | +*/ |
| 46 | + |
| 47 | +for (let i = 1; i <= 12; i++) { |
| 48 | + // console.log("number" + i); |
| 49 | + |
| 50 | + for (let j = 1; j <= 12; j++) { |
| 51 | + console.log(`${i} * ${j} = ${i * j}`); |
| 52 | + } |
| 53 | + |
| 54 | + console.log("number" + i); |
| 55 | +} |
| 56 | + |
| 57 | +// NOTE - the looop above can be used to solve multiplication tables stuffs |
0 commit comments