SlideShare a Scribd company logo
1 of 23
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

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
 
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
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
kimberly_Bm10203
 

Similar to Loops (Refined).pptx (20)

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
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 

Recently uploaded

CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)
Wonjun Hwang
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Recently uploaded (20)

Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
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