-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicLoops.js
86 lines (66 loc) · 1.58 KB
/
BasicLoops.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// while
let num = 1; // initilization
while(num <= 10){ // condition check
console.log(num * 2); // execute
num = num + 1; // increment
}
// short hand expression
// num = num + num1; ~ num =+ num1;
// num = num + 1; ~ num++;
// do..while
let num1 = 1; // initilization
do {
console.log(num1 * 2);
num1++; // increment
} while(num1 > 10) // condition check
// change to < here if you want table of 2
// only 2 prints
// for
for(let num2 = 1; num2 <= 10; num2++){
console.log(num2 * 2); // execute
}
// avoid infinite loop
console.log("reverse loop");
for(let num2 = 10; num2 >= 1; num2--){
console.log(num2 * 2); // execute
}
// nested loop
console.log("nested loop");
for(let i = 1; i <= 5; i++){
for(let j = 1; j <= 5; j++){
console.log(j);
}
}
console.log("star loop");
let str = "";
for(let i = 1; i <= 10; i++){
for(let j = 1; j <= i; j++){
str = str + "*";
}
str = str + "\n";
}
console.log(str);
// Falsy values - null, undefined, "", '', ``, 0, false\
// break stmt
console.log("break in loop");
// outer loop in not effected
// break will come out of the entire same loop only
for(let i = 1; i <= 5; i++){
for(let j = 1; j <= 5; j++){
if(j == 3){
break;
}
console.log(j);
}
}
// continue stmt
// no effect on outer loop
console.log("continue in loop");
for(let i = 1; i <= 5; i++){
for(let j = 1; j <= 5; j++){
if(j % 2 !== 0){ // j is odd = even case should run
continue; // skip the current iteration only
}
console.log(j);
}
}