SlideShare a Scribd company logo
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
TYPES OF LOOPS
• For loops
• For-in loops (Objects)
• For-of loops (Arrays)
• While loops
• Do-While loops
For Loops
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
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>
<!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>
For-in Loops
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
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)
<!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>
For-of Loops
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.
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>
<!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>
While Loops
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.
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>
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>
Do-While Loops
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.
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.
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

More Related Content

Similar to Loops (Refined).pptx

270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
UdhayaKumar175069
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
ummeafruz
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
ray143eddie
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
Alefya1
 
The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.2 book - Part 6 of 181The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.2 book - Part 6 of 181
Mahmoud Samir Fayed
 
10. session 10 loops and arrays
10. session 10   loops and arrays10. session 10   loops and arrays
10. session 10 loops and arrays
Phúc Đỗ
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
Chhom Karath
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
NagaLakshmi_N
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
Tanzeel Ahmad
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
jewelyngrace
 
Solution manual of shell programming assignment 2
Solution manual of shell programming assignment 2Solution manual of shell programming assignment 2
Solution manual of shell programming assignment 2
Kuntal Bhowmick
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
Niladri Karmakar
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
kimberly_Bm10203
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
Tiji Thomas
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
MuskanSony
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
yap_raiza
 
XAML/C# to HTML/JS
XAML/C# to HTML/JSXAML/C# to HTML/JS
XAML/C# to HTML/JS
Michael Haberman
 

Similar to Loops (Refined).pptx (20)

270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.2 book - Part 6 of 181The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.2 book - Part 6 of 181
 
10. session 10 loops and arrays
10. session 10   loops and arrays10. session 10   loops and arrays
10. session 10 loops and arrays
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Solution manual of shell programming assignment 2
Solution manual of shell programming assignment 2Solution manual of shell programming assignment 2
Solution manual of shell programming assignment 2
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
XAML/C# to HTML/JS
XAML/C# to HTML/JSXAML/C# to HTML/JS
XAML/C# to HTML/JS
 

Recently uploaded

System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 

Recently uploaded (20)

System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 

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