Skip to content

Commit 99ea27a

Browse files
committed
Loop(Cntrol Flow)
1 parent 72c43ff commit 99ea27a

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

08. Loop(Cntrol Flow)/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1 id="showMessage"></h1>
10+
<!-- link javacript file -->
11+
<script src="js/script.js"></script>
12+
13+
</body>
14+
</html>

08. Loop(Cntrol Flow)/js/script.js

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
// JavaScript Loops
2+
// Loops can execute a block of code a number of times.
3+
// Loops are handy, if you want to run the same code over and over again, each time with a different value.
4+
5+
// JavaScript supports different kinds of loops:
6+
7+
// for - loops through a block of code a number of times
8+
// for/in - loops through the properties of an object
9+
// for/of - loops through the values of an iterable object
10+
// while - loops through a block of code while a specified condition is true
11+
// do/while - also loops through a block of code while a specified condition is true
12+
13+
// The While Loop
14+
// The while loop loops through a block of code as long as a specified condition is true.
15+
16+
// Syntax
17+
// while (condition) {
18+
// code block to be executed
19+
// }
20+
let i = 0;
21+
while (i < 10) {
22+
console.log(i);
23+
i++;
24+
}
25+
26+
// excersice:
27+
//Print even numbers 1 to 10
28+
i = 1;
29+
console.log("Even numbers 1 to 10");
30+
while (i <= 10) {
31+
if (i % 2 === 0) console.log(i);
32+
i++;
33+
}
34+
35+
36+
// The Do While Loop
37+
// The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
38+
39+
// Syntax
40+
// do {
41+
// // code block to be executed
42+
// }
43+
// while (condition);
44+
45+
// The example below uses a do while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
46+
console.log("Do While Loop:");
47+
i = 1;
48+
49+
do {
50+
console.log(i);
51+
i++;
52+
} while (i > 10);
53+
54+
55+
// The For Loop
56+
// The for statement creates a loop with 3 optional expressions:
57+
// Syntax
58+
// for (expression 1; expression 2; expression 3) {
59+
// // code block to be executed
60+
// }
61+
62+
63+
// Expression 1 is executed (one time) before the execution of the code block.
64+
// Expression 2 defines the condition for executing the code block.
65+
// Expression 3 is executed (every time) after the code block has been executed.
66+
67+
console.log("For Loop:");
68+
for (let i = 0; i < 5; i++) {
69+
console.log(i);
70+
}
71+
72+
// excercise: add 1 to 10
73+
74+
console.log("For Excercise 1: ");
75+
let sum = 0;
76+
for (let i = 1; i <= 10; i++) {
77+
sum += i;
78+
}
79+
80+
console.log(sum);
81+
82+
console.log("And you can omit expression 1 (like when your values are set before the loop starts");
83+
i = 2;
84+
85+
for (; i <= 10; i++) {
86+
console.log(i);
87+
}
88+
89+
// Expression 2
90+
// Often expression 2 is used to evaluate the condition of the initial variable.
91+
// This is not always the case. JavaScript doesn't care. Expression 2 is also optional.
92+
// If expression 2 returns true, the loop will start over again. If it returns false, the loop will end.
93+
// If you omit expression 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser. Read about breaks in a later chapter of this tutorial.
94+
95+
console.log("Ignoring expression 2:")
96+
i = 3;
97+
98+
for (; ; i++) {
99+
if (i == 7) break;
100+
console.log(i);
101+
}
102+
103+
104+
// Expression 3
105+
// Often expression 3 increments the value of the initial variable.
106+
// This is not always the case. JavaScript doesn't care. Expression 3 is optional.
107+
// Expression 3 can do anything like negative increment (i--), positive increment (i = i + 15), or anything else.
108+
// Expression 3 can also be omitted (like when you increment your values inside the loop):
109+
console.log("Ignoring expression 3:")
110+
i = 3;
111+
112+
for (; ;) {
113+
if (i == 7) break;
114+
console.log(i);
115+
i++;
116+
}
117+
118+
119+
// Loop Scope
120+
// Using var in a loop:
121+
var n = 5;
122+
123+
for (var n = 0; n < 10; n++) {
124+
console.log(n);
125+
}
126+
console.log("End Loop");
127+
console.log(n);
128+
129+
130+
// Using let in a loop:
131+
132+
133+
let o = 5;
134+
135+
for (let o = 0; o < 10; o++) {
136+
console.log(o);
137+
}
138+
console.log("End Loop");
139+
console.log(o);
140+
141+
// In the first example, using var, the variable declared in the loop redeclares the variable outside the loop.
142+
143+
// In the second example, using let, the variable declared in the loop does not redeclare the variable outside the loop.
144+
145+
// When let is used to declare the i variable in a loop, the i variable will only be visible within the loop
146+
147+
// The For In Loop
148+
// The JavaScript for in statement loops through the properties of an Object:
149+
// Syntax
150+
// for (key in object) {
151+
// // code block to be executed
152+
// }
153+
154+
// For in trough index of array and string and propery of object
155+
156+
157+
// Example
158+
// for in through the property of an Object. With the property we can access the property value
159+
160+
const person = { fname: "John", lname: "Doe", age: 25 };
161+
console.log("For in loop using with object: ");
162+
for (let x in person) {
163+
console.log(person[x]);
164+
}
165+
console.log("For in loop using with array: ");
166+
const numbers = [45, 4, 9, 16, 25];
167+
for (let x in numbers) {
168+
console.log(numbers[x]);
169+
}
170+
171+
console.log("For in loop using with String: ");
172+
const line = "Hello, Banglades";
173+
for (let ch in line) {
174+
console.log(line[ch]);
175+
}
176+
177+
// Array.forEach()
178+
// The forEach() method calls a function (a callback function) once for each array element.
179+
180+
console.log("For Each Loop: ");
181+
182+
numbers.forEach(myFunction);
183+
184+
function myFunction(value, index, array) {
185+
console.log(value, index, array);
186+
}
187+
188+
// The For Of Loop
189+
190+
// The JavaScript for of statement loops through the values of an iterable object.
191+
// It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:
192+
193+
// But it not working for Object
194+
195+
// Syntax
196+
// for (variable of iterable) {
197+
// // code block to be executed
198+
// }
199+
200+
//It give us value of each index in the array or string
201+
console.log("Looping over an Array");
202+
const cars = ["BMW", "Volvo", "Mini"];
203+
204+
for (let x of cars) {
205+
console.log(x);
206+
}
207+
208+
console.log("Looping over a String");
209+
let language = "JavaScript";
210+
211+
for (let x of language) {
212+
console.log(x);
213+
}
214+
215+
// JavaScript Break and Continue
216+
217+
// The break statement "jumps out" of a loop.
218+
// The continue statement "jumps over" one iteration in the loop.
219+
console.log("The break statement can also be used to jump out of a loop:");
220+
221+
for (let i = 0; i < 10; i++) {
222+
if (i === 3) { break; }
223+
console.log(i);
224+
}
225+
226+
// The Continue Statement
227+
// The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
228+
229+
console.log("This example skips the value of 3:");
230+
for (let i = 0; i < 10; i++) {
231+
if (i === 3) { continue; }
232+
console.log(i);
233+
}
234+
235+
236+
// Excercise
237+
console.log("Sum of the series of square Numbers: ");
238+
239+
let input = parseInt(prompt("Please Enter The value: "));
240+
241+
let res = 0;
242+
let str = "";
243+
for(let i = 1; i <=input; i++) {
244+
let val = i * i;
245+
console.log(val);
246+
res += val;
247+
str += val.toString();
248+
if(i === input) continue;
249+
str += " + "
250+
}
251+
252+
console.log(str, "= ", res);

0 commit comments

Comments
 (0)