L5, Loop and Iteration, CSE 202, BN11
L5, Loop and Iteration, CSE 202, BN11
Conditional
statement while loop,
for loop
do..while loop
.
2
Loop
Loops offer a quick and easy way to do something repeatedly. We
will discuss:
for loop
while loop
do..while loop
for..of loop
for..in loop
forEach() method
3
for loop
Syntax:
for (initialization; condition; update)
statement;
The for...of statement creates a loop Iterating over iterable objects (including
Array, Map, Set, arguments object and so on), invoking a custom iteration hook
with statements to be executed for the value of each distinct property.
for..of is for iterting iterables (array elements)
Example:
const array = [10, 20, 30];
for (let value of array){
console.log(value); //output: 10, 20, 30
}
7
for..in loop
Syntax:
for (variable in object)
statement;
The for...in statement iterates a specified variable over all the enumerable
properties of an object. For each distinct property, JavaScript executes the
specified statements.
for..in is for iterating over object properties.
Example:
const person = {name:"Alice", age: 30};
for (let key in person){
console.log(key+":"+person[key]); //output:
name: Alice, age:30)
}
8
forEach() method
Syntax:
array.forEach(function(currentValue, index, arr)
Example: Example:
let sum = 0; const numbers = [65, 44, 12, 4];
const numbers = [65, 44, 12, 4]; numbers.forEach(myFunction)
numbers.forEach(myFunction);
function myFunction(item, index, arr) {
function myFunction(item) { arr[index] = item * 10;
sum += item; //output: compute sum //output: multiply each element by 10
} } 9
Worked out example
Example 5: Program to calculate the summation of the elements of an array using
loop statement. (calculate total skin friction capacity (kN) for a bored pile, given
skin friction =[20, 25, 30, 26] using loop statement.)
10
Interface
11
html codes
<!DOCTYPE html>
<html lang="en"> <body>
<head> <div class="container" id="container">
<meta charset="UTF-8">
<meta name="viewport" </div>
content="width=device-width, initial- <script src="app.js"></script>
scale=1.0"> </body>
<title>Skin Friction Capacity</title> </html>
<link rel="stylesheet" href="style.css">
</head>
12
css codes
body{
margin: 0px;
background-color: rgba(145, 143, 30, padding: 5px;
0.7); display: flex;
} flex-direction: column;
justify-content: center;
.container{ align-items: center;
background-color:rgba(145, 143, 30, 0.4); box-shadow: 0 0 5px 1px rgba(0,0,0,0.5);
height: 200px; border-radius: 10px;
width: 400px; font-size: larger;
margin: 20px auto;
}
13
JS codes
function display(){
const containerEl =
containerEl.innerText = "Total friction
document.getElementById("container");
capacity of the pile:" + calculation() +"
const skinFrictions = [20, 25, 30, 26];
kN";
}
let frictionCapacity = 0;
display();
function calculation(){
for (let skinFriction of skinFrictions)
frictionCapacity += skinFriction;
return frictionCapacity;
}
14
Class tasks
Task 5.1: Program to calculate %cumulative retained in a sieve analysis
of fine aggregate, given %retained = [10, 23, 12, 11] using loop
statement.
Task 5.2: Program to calculate %cumulative frequency for a spot
speed, given %frequency = [12, 17, 25] using loop statement.
Task 5.3: Program to calculate total man-hr for carpenters in a housing
project, given man-hr= [10, 17, 45, 28, 20] using loop statement.
Task 5.4: Program to calculate total COD loading per day (mg/l), given
COD loading per day= [240, 310, 570] using loop statement.
Task 5.5: Program to calculate total un-factored load (ton) of a high rise
building, given dead load, live load, wind load and earthquake load =
[500, 200, 100, 50] respectively using loop statement.
15
End of the Lecture
16