-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_for_loop.js
70 lines (58 loc) · 2.24 KB
/
1_for_loop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// `for` loop
// What
// - A for loop in JavaScript is a programming construct that allows you to repeat a set of instructions a certain number of times. It's great for automating tasks that need to be done repeatedly.
// Why
// - Imagine you have a task you need to do over and over again. Instead of writing the same code multiple times, a for loop helps you perform that task a specific number of times, making your code more efficient and easier to manage.
// How
// - Initialization: You start by defining a variable and giving it an initial value. This variable will keep track of the loop's progress.
// - Condition: You specify a condition that, as long as it's true, the loop will continue to run. This condition is checked before every loop iteration.
// - Iteration: After each iteration of the loop, the loop variable is updated according to a certain pattern.
// - Instructions: Inside the loop, you put the instructions you want to repeat.
// - Update: After each iteration, the loop variable is updated following the specified pattern. The loop continues to run as long as the condition is true.
// Note
// - Make sure that your loop's condition becomes false at some point; otherwise, your loop will run indefinitely, causing your program to crash.
// Syntax
// for (initialization; condition; iteration) {
// Instructions to repeat
// }
// Example
for (let index = 0; index <= 10; index++) {
console.log(index);
}
for (let index = 0; index <= 10; index++) {
if (index === 5) {
console.log("This is number 5");
}
console.log(index);
}
// Nested for loop
for (let i = 1; i <= 10; i++) {
// console.log(`Outer loop: ${i}`);
for (let j = 1; j <= 10; j++) {
// console.log(`Inner loop: ${j}`);
console.log(`${i} * ${j} = ${i * j}`);
}
}
// For loop for array
const myArray = [0, 1, 2, 3, 4, 5];
console.log(myArray);
for (let index = 0; index < myArray.length; index++) {
const element = myArray[index];
console.log(element);
}
// Break
for (let index = 0; index <= 10; index++) {
if (index === 5) {
console.log("Number 5 detected");
break;
}
console.log(index);
}
// Continue
for (let index = 0; index <= 10; index++) {
if (index === 5) {
console.log("Number 5 detected");
continue;
}
console.log(index);
}