-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.js
66 lines (55 loc) · 1.5 KB
/
loops.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
// LOOPS
// for
// while
// do while
// for of loops through values
// for in loops through properties
// FOR
for (let i=1; i<10; i++)
{
// code for checking all even values replace with i%2 !== 0 to check odd values
if(i%2 ===0)
{
console.log("i will be " + i);
}
}
// WHILE LOOP
// true will execute
// let x = 1;
// while(x > 0){
// console.log(x);
// x++;
// }
// A false will not execute
// let x = null;
// while(x =null){
// console.log(x);
// x++;
// }
// Terminating while loop
let b = 10;
while (b >=10 && b <= 40)
{
console.log("b will be" + b);
b += 5; // this will terminate infinity loop
}
// DO WHILE => The do...while loop is similar to while, in that it will execute a block of code based on a condition or set of conditions. The difference, however, is that it will always execute at least once. The reason is that the condition isn't evaluated until after the code block executes the first time:
// eg making a passowrd
let count = 1;
do {
console.log("count will be "+ count);
count += 2;
} while (count <= 5);
// password = false
// do {
// console.log("Enter a Password!");
// } while(password = true )
// FOR OF => printing indivual items for array
// let fruits =["mangoes","apples","oranges"]
// // displaying everything in an Array
// console.log(fruits);
// // displaying an individual
// for (let fruit of fruits)
// {
// console.log(fruits[0])
// }