Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Loops (Refined).pptx
1.
2. WHAT ARE LOOPS?
• Loops are very important in programming. It allows a
person to repeat a code over and over again.
• Loop involves computing a programmed sequence of
instructions that is repeated until or while a particular
condition is satisfied
3. TYPES OF LOOPS
• For loops
• For-in loops (Objects)
• For-of loops (Arrays)
• While loops
• Do-While loops
5. FOR LOOPS
For Loops are loops that repeat code for a certain amount of time. The
syntax of For Loops is:
<script>
for (let i = n; i > x; i++){
// code block to be executed
}
</script>
So let’s do some examples
6. EXAMPLES OF FOR LOOPS
1. Write a code that writes numbers from 1 to 10.
You can write each code but that will be stressful. This is where
the use of loops comes in. Let’s execute the above code using for
loops.
<script>
for (let i = 0; i < 10; i++){
document.write((i+1) + “<br>”);
}
</script>
7. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Write a code that writes numbers from 1 to 10</title>
</head>
<body>
<script>
for (let i = 0; i < 10; i++){
document.write((i+1) + "<br>");
}
</script>
</body>
</html>
9. FOR-IN LOOPS
• The JavaScript for in statement loops through the properties of an Object.
The syntax is:
<script>
for (key in object) {
// code block to be executed
}
</script>
Lets look at some examples
10. EXAMPLES OF FOR-IN LOOPS
1. Write a code that will output a person’s firstname, lastname and age
separated by spaces. (The code must be derived from an object)
It is going to iterate through all the keys inside objects and you can use
the keys to get the values.
<script>
let person = {fname : “Simi”, lname : "David", age : 25};
let text = "";
for (let x in person) {
text = text + (person[x] + “ ”);
}
document.write(text)
11. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code to write the first name, last name and age</title>
</head>
<body>
<script>
const person = {fname : "Simi", lname : "David", age : 25};
let text = "";
for (let x in person) {
text = text + (person[x] + " ");
}
document.write(text)
</script>
</body>
</html>
13. FOR-OF LOOPS
The JavaScript for of statement can loop over the properties of an Array.
Here’s the syntax:
for (variable of array) {
// code to be executed
}
Let’s start some examples.
14. EXAMPLE OF FOR-OF LOOPS
1. Find and output the total of the numbers in a given array.
<script>
const numbers = [45, 4, 9, 16, 25];
let sum = 0;
for (let x of numbers) {
sum = sum + x;
}
document.write(sum)
</script>
15. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code to find the sum of numbers in a given array</title>
</head>
<body>
<script>
const numbers = [45, 4, 9, 16, 25];
let sum = 0;
for (let x of numbers) {
sum = sum + x;
}
document.write(sum)
</script>
</body>
</html>
17. WHILE LOOPS
The JavaScript while statement can loop forever until a particular condition
is met. Here’s the syntax:
while (condition) {
// code to be executed
}
Let’s start some examples.
18. WHILE LOOPS
1. Make a code that counts from 1 to 10 using while loops.
<script>
let x = 1;
while(x <= 10){
document.write(x + “<br>”)
x = x + 1;
}
</script>
19. WHILE LOOPS
2. Write a code that reduces a number by 0.5 as long as it is still
greater than zero. Output the number of times it took
<script>
let count = 0;
while (num > 0) {
num = num - 0.5;
count++;
}
document.write(count);
</script>
21. DO-WHILE LOOPS
Do while loops check the condition after the block of code is executed. This
control structure can be known as a post-test loop. It means even if the
condition has already been met the code must run once. Here’s the syntax:
do{
// code to be executed
}
while (condition)
Let’s start some examples.
22. DO-WHILE LOOPS
1. Write a code that outputs the numbers 1 to any number of your choice.
<script>
let a = 1;
let n = Number(prompt(“Pick a number”))
do{
document.write(a)
a++;
}
while(a < n)
</script>
If this was a while loop and someone wrote 0 it will not even start
but a do-while loop will do it once and write 1 before it checks and stops.
23. ASSIGNMENT
1.Write a code using for of loop that outputs
the product of a set of numbers [20 , 5 , 4 ,
13]
2.Bola is trying to go to her friend, Fola’s
house. The arrangement of houses are
[“Bola”,”Tolu”,”Peter”,”Fiyin”,”Fola”,”David”,”Is
hola”]. Use a while loop to tell Bola how
many metres she would travel (assuming the