-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhileLoopIntro.js
More file actions
107 lines (81 loc) · 2.45 KB
/
whileLoopIntro.js
File metadata and controls
107 lines (81 loc) · 2.45 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
// Conditionals :: Output - Only for one time.
var roastGiven = 0;
if (roastGiven < 7) {
console.log("Give me a Roast, please.");
}
// Output || Give me a Roast, please.
*/
/*
// Same type of task like the above one, but for repeatedly result, using 'while' loop :: Infinity Loop.
var roastGiven = 0;
while (roastGiven < 7) {
console.log("Give me a Roast, please.");
}
// Output || Give me a Roast, please. [Infinite times]
*/
/*
// Above task of 'while' loop :: off the 'Infinite' output of 'while' loop.
var roastGiven = 0;
while (roastGiven < 7) {
console.log("Give me a Roast, please.");
// roastGiven = roastGiven + 1;
// Shorthand of the above previous line of code.
// roastGiven += 1;
// Another shorthand of the each line of the above both previous lines of codes.
roastGiven++;
}
*/
/*
Output [Loop iteration - 7 times] ||
Give me a Roast, please.
Give me a Roast, please.
Give me a Roast, please.
Give me a Roast, please.
Give me a Roast, please.
Give me a Roast, please.
Give me a Roast, please.
*/
/*
// For the better understanding of the loop iteration of the above code, the above code can be written as follows.
var roastGiven = 0;
while (roastGiven < 7) {
console.log(roastGiven, "Give me a Roast, please.");
roastGiven++;
}
*/
/*
// Output [Loop iteration - 7 times (0 - 6)] ||
0 Give me a Roast, please.
1 Give me a Roast, please.
2 Give me a Roast, please.
3 Give me a Roast, please.
4 Give me a Roast, please.
5 Give me a Roast, please.
6 Give me a Roast, please.
*/
/*
// In respect with the above program, if we want to display/print the loop iteration number from '1 to 7', instead of the number from '0 to 6' without changing the condition, just write one line of code inside the loop body in one-step earlier, to increase/decrease the value of the loop variable in the very beginning.
var roastGiven = 0;
while (roastGiven < 7) {
roastGiven++;
console.log(roastGiven, "Give me a Roast, please.");
}
*/
/*
// Output [Loop iteration - 7 times (1 - 7)] ||
1 Give me a Roast, please.
2 Give me a Roast, please.
3 Give me a Roast, please.
4 Give me a Roast, please.
5 Give me a Roast, please.
6 Give me a Roast, please.
7 Give me a Roast, please.
*/
/*
// Rules of writing 'while' loop:
1. Loop variable.
2. Condition inside 'while'.
3. Loop body.
4. Change (increment/decrement) the value of the loop variable. Never ignore this changing step (otherwise, the loop will become infinite).
*/