SlideShare a Scribd company logo
LOOPS AND ARRAYS
       Session 10
Objectives
   Describe while loop.
   Explain for loop.
   Describe do-while loop.
   Explain break and continue statements.
   Describe the methods of Array object.
What is a loop?
   Loop is a section of code in a program which is
    executed repeatedly, until a specific condition is
    satisfied.
   There are three type of loop structures:

             The while loop

          The do-while loop

              The for loop
The while loop
   The while loop executes a
    block of code as long as the
    given condition remains        Conditio
                                                false

    true.                             n



 Syntax:
                                         true
while (condition)
{                                  Execute
                                    body            Exit loop
  statement(s);                    of loop

}
The while loop: Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>while loop demo</title>
   <script language="javascript" type="text/javascript">
         var n=20;
         var i=0;
         document.write("Display the odd number <br>");
         while(i<=n)
         {
            if(i%2 == 0)
                  document.write(i + "t");
            i++;
         }
   </script>
   </head>
   <body>
   </body>
</html>
The do-while loop
   The do-while loop is similar to the
    while loop, but the do-while loop                 do

    evaluates the condition at the end of
    the loop. So that, the do-while loop           Execute
                                                    body
    executes at least once.                        of loop

 Syntax:
do                                          true
                                                    Conditio
                                                       n
{
  statement(s);                                            false

}while(condition);                                 Exit loop
The do-while loop: Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>while loop demo</title>
   <script language="javascript" type="text/javascript">
         var n=20;
         var i=0;
         document.write("Display the even number <br>");
         do
         {
            if(i%2 == 0)
                  document.write(i + "t");
            i++;
         } while(i<=n);
   </script>
   </head>
   <body>
   </body>
</html>
The for loop
 Syntax:
for(initialization; condition; increment/decrement)
{
  statement(s);
}
   The initialization is an assignment statement that sets the
    loop control variable, before entering the loop.
    The condition is a relational expression, which
    determines, when the loop will exit.
    The increment/decrement defines how the loop control
    variable changes, each time the loop is executed.
The for loop: Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>for loop demo</title>
   <script language="javascript" type="text/javascript">
         var n=20;
         var i=0;
         document.write("Display the even number <br>");
         for(i=0; i<n; i++)
         {
            if(i%2 == 0)
                  document.write(i + "t");
         }
   </script>
   </head>
   <body>
   </body>
</html>
break statement
   The break statement can be used in the switch-
    case and loop constructs. It is used to exit the
    loop without evaluating the specified condition.

    for(initialization; condition; increment/decrement)
    {
       ….
       if(condition)
           break;
       …
    }
    ...
break statement: Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>Using break statement</title>
   <script language="javascript" type="text/javascript">
         var n = prompt("Enter the number n:");
         var i=0;
         for(i=2; i<n; i++)
         {
                  if(n % i == 0)
                  {
                           document.write(n + " is not a prime number.");
                           break;
                  }
         }
         if(i == n)
            document.write(n + " is prime number.");
   </script>
   </head>
</html>
continue statement
   The continue statement is mostly used in the loop
    constructs. It is used to terminate the current
    execution of the loop and continue with the next
    repetition by returning the control to the beginning of
    the loop.
        for(initialization; condition; increment/decrement)
        {
           ….
           if(condition)
               continue;
           …
        }

        ...
continue statement: Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>continue statement demo</title>
   <script language="javascript" type="text/javascript">
         var n=20;
         var i=0;
         document.write("Display the even numbers <br>");
         for(i=0; i<=n; i++)
         {
                  if(i%2 == 1)
                           continue;
                  else
                           document.write(i + "t");
         }
   </script>
   </head>
</html>
Single-dimensional arrays
   An array is a collection of values stored in adjacent
    memory locations. These array values are referenced
    using a common array name.
   The values of array are the same data type. These values
    can be accessed by using the subscript or index numbers.
    The index determines the position of element in the array
    list.
   In a single-dimensional array, the elements are stored in a
    single row in the located memory.
   In JavaScript, the first element has the index number zero.
                                                  Index
                                          Value
Declaring arrays
There are three ways to declaring an array:
 Using the Array object: declare an array by using the new
  operator, and then initialize the individual array elements:
    var arr = new Array(3);
    arr[0] = “Single”;
    arr[1] = “Married”;
    arr[2] = “Divorced”;
   Initialize the array variable at the time of declaration:
    var arr = new Array(„Single‟,‟Married‟,‟Divorced‟);
   Creates an array without using Array object:
    var arr = {„Single‟,‟Married‟,‟Divorced‟};
Single-dimensional arrays - Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>Single-dimensional array</title>
         <script language="javascript" type="text/javascript">
            var arr = new Array("Lundi", "Mardi", "Mercredi", "Jeudi",
                  "Vendredi", "Samedi","Dimanche");
            document.write("The days in a week <br>");
            for(i=0; i<arr.length; i++)
               {
                  document.write(arr[i] + '<BR>');
               }
         </script>
   </head>
</html>
Multi-dimensional arrays
   A multi-dimensional array stores a combination of values of
    a single type in two or more dimensions.
   A two-dimensional array represents as rows and columns
    similar to a table.
   JavaScript does not directly support two-dimensional array.
    You can create two-dimensional array by creating an array
    of arrays. You first declare an array and then create another
    array to each element of the main array.
    var emp= new Array(3);
    emp[0] = new Array(„John‟,‟300‟);
    emp[1] = new Array(„David‟,‟400‟);
    emp[2] = new Array(„Richard‟,‟500‟);
Multi-dimensional arrays - Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>Single-dimensional array</title>
      <script language="javascript" type="text/javascript">
         var emp = new Array(3);
         emp[0] = new Array('Huynh Bay','1900');
         emp[1] = new Array('Minh Thanh','900');
         emp[2] = new Array('Tan Hung','800');
         document.write('<table border=1> <tr> <th>Name</th>   <th>Salary</th>
                  </tr>');
         for(var i=0; i<emp.length; i++)
         {
            document.write('<tr>');
            for(var j=0; j<emp[i].length; j++)
               document.write('<td>' + emp[i][j] + '</td>');
            document.write('</tr>');
         }
         </script>
   </head>
</html>
Array methods
   An array is a set of values grouped together an identified by
    a single name. In JavaScript, an Array allows you to create
    arrays. It provides the length property that is used to
    determine the number of elements in an array.
   The various methods allow you to access and manipulate
    the array elements:
    - concat: combines one or more array variables.
    - join: joins all the array elements into a string.
    - pop: retrieves the last element of an array.
    - push: appends one or more elements to the end of an array.
    - sort: sorts the array elements in alphabetical order.
    - reverse: retrieves the elements from the last to the first index
    position.
    - toString: converts the array elements into string.
Array methods - Demo
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <title>Using method in array</title>
         <script language="javascript" type="text/javascript">
            var days = new Array('Monday','Tuesday','Wednesday');
            document.write('Number of days: ' + days.length + '<br>');
            document.write('The days: ' + days.join(', ') + '<br>');
            document.write('The days after adding more: ' +
                  days.push('Thursday','Friday','Saturday') + '<br>');
            document.write('The days after sorting:' + days.sort() + '<br>');
            document.write('The days after sorting in desceding order:' +
                  days.reverse() + '<br>');
         </script>
   </head>
</html>
for … in Loop
   The for…in loop is an extension of for loop. It enables you
    to perform specific actions on the array objects. The loop
    reads every element in the specified array and executes a
    block of code once for each element in the array.
   Example:
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Using method in array</title>
           <script language="javascript" type="text/javascript">
              var arr = new Array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi",
           "Samedi","Dimanche");
              document.write('THE DAYS IN A WEEK: <BR>');
              for(var i in arr)
              {
                     document.write(arr[i] + '<br>');
              }
           </script>
    </head>
</html>
Summary
   Loop statements allow to execute the same block
    of code multiple times depending whether the
    specified condition is sastified or not.
   Array is a collection of values of the same type.
   Javascript support three types of loop:
       While loop
       For loop
       Do….while loop
   Jump statements: break, continue
                                 Building Dynamic Websites/Session 1/ 22 of 16
Summary…
   There are two types arrays:
       Single dimensional arrays
       Multiple dimensional arrays
   For….in loop is an extension of the for loop.




                                      Building Dynamic Websites/Session 1/ 23 of 16

More Related Content

What's hot

Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
Bedis ElAchèche
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
shreesenthil
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )
Victor Verhaagen
 
Swift internals
Swift internalsSwift internals
Swift internals
Jung Kim
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
msemenistyi
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
Simon Willison
 
Java script
Java scriptJava script
Java script
Adrian Caetano
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
Hardened JavaScript
Hardened JavaScriptHardened JavaScript
Hardened JavaScript
KrisKowal2
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
Todd Anglin
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
T11 Sessions
 

What's hot (20)

Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )
 
Swift internals
Swift internalsSwift internals
Swift internals
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
 
Java script
Java scriptJava script
Java script
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Hardened JavaScript
Hardened JavaScriptHardened JavaScript
Hardened JavaScript
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Bottom Up
Bottom UpBottom Up
Bottom Up
 

Similar to 10. session 10 loops and arrays

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
Janssen Harvey Insigne
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
Denis Ristic
 
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdfjavascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
AlexShon3
 
Java scripts
Java scriptsJava scripts
Java scripts
Capgemini India
 
Java script
 Java script Java script
Java scriptbosybosy
 
JavaScript lesson 1.pptx
JavaScript lesson 1.pptxJavaScript lesson 1.pptx
JavaScript lesson 1.pptx
MuqaddarNiazi1
 
Javascript
JavascriptJavascript
Javascript
orestJump
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoftch samaram
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to Programming
Cindy Royal
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
Fin Chen
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basicsH K
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
Reem Alattas
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
Manvendra Singh
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
Ajay Khatri
 
Java tut1
Java tut1Java tut1
Java tut1
Sumit Tambe
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
Graham Royce
 

Similar to 10. session 10 loops and arrays (20)

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
JavaScript
JavaScriptJavaScript
JavaScript
 
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
 
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdfjavascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
 
Java scripts
Java scriptsJava scripts
Java scripts
 
Java script
 Java script Java script
Java script
 
JavaScript lesson 1.pptx
JavaScript lesson 1.pptxJavaScript lesson 1.pptx
JavaScript lesson 1.pptx
 
Javascript
JavascriptJavascript
Javascript
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoft
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to Programming
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Javascript - Beyond-jQuery
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basics
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 

More from Phúc Đỗ

15. session 15 data binding
15. session 15   data binding15. session 15   data binding
15. session 15 data bindingPhúc Đỗ
 
14. session 14 dhtml filter
14. session 14   dhtml filter14. session 14   dhtml filter
14. session 14 dhtml filterPhúc Đỗ
 
13. session 13 introduction to dhtml
13. session 13   introduction to dhtml13. session 13   introduction to dhtml
13. session 13 introduction to dhtmlPhúc Đỗ
 
12. session 12 java script objects
12. session 12   java script objects12. session 12   java script objects
12. session 12 java script objectsPhúc Đỗ
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statementsPhúc Đỗ
 
08. session 08 intoduction to javascript
08. session 08   intoduction to javascript08. session 08   intoduction to javascript
08. session 08 intoduction to javascriptPhúc Đỗ
 
07. session 07 adv css properties
07. session 07   adv css properties07. session 07   adv css properties
07. session 07 adv css propertiesPhúc Đỗ
 
06. session 06 css color_andlayoutpropeties
06. session 06   css color_andlayoutpropeties06. session 06   css color_andlayoutpropeties
06. session 06 css color_andlayoutpropetiesPhúc Đỗ
 
05. session 05 introducing css
05. session 05   introducing css05. session 05   introducing css
05. session 05 introducing cssPhúc Đỗ
 
04. session 04 working withformsandframes
04. session 04   working withformsandframes04. session 04   working withformsandframes
04. session 04 working withformsandframesPhúc Đỗ
 
03. session 03 using lists and tables
03. session 03   using lists and tables03. session 03   using lists and tables
03. session 03 using lists and tablesPhúc Đỗ
 
02. session 02 working with html elements
02. session 02   working with html elements02. session 02   working with html elements
02. session 02 working with html elementsPhúc Đỗ
 
15. session 15 data binding
15. session 15   data binding15. session 15   data binding
15. session 15 data bindingPhúc Đỗ
 
01. session 01 introduction to html
01. session 01   introduction to html01. session 01   introduction to html
01. session 01 introduction to htmlPhúc Đỗ
 

More from Phúc Đỗ (15)

15. session 15 data binding
15. session 15   data binding15. session 15   data binding
15. session 15 data binding
 
14. session 14 dhtml filter
14. session 14   dhtml filter14. session 14   dhtml filter
14. session 14 dhtml filter
 
13. session 13 introduction to dhtml
13. session 13   introduction to dhtml13. session 13   introduction to dhtml
13. session 13 introduction to dhtml
 
12. session 12 java script objects
12. session 12   java script objects12. session 12   java script objects
12. session 12 java script objects
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
09. session 9 operators and statements
09. session 9   operators and statements09. session 9   operators and statements
09. session 9 operators and statements
 
08. session 08 intoduction to javascript
08. session 08   intoduction to javascript08. session 08   intoduction to javascript
08. session 08 intoduction to javascript
 
07. session 07 adv css properties
07. session 07   adv css properties07. session 07   adv css properties
07. session 07 adv css properties
 
06. session 06 css color_andlayoutpropeties
06. session 06   css color_andlayoutpropeties06. session 06   css color_andlayoutpropeties
06. session 06 css color_andlayoutpropeties
 
05. session 05 introducing css
05. session 05   introducing css05. session 05   introducing css
05. session 05 introducing css
 
04. session 04 working withformsandframes
04. session 04   working withformsandframes04. session 04   working withformsandframes
04. session 04 working withformsandframes
 
03. session 03 using lists and tables
03. session 03   using lists and tables03. session 03   using lists and tables
03. session 03 using lists and tables
 
02. session 02 working with html elements
02. session 02   working with html elements02. session 02   working with html elements
02. session 02 working with html elements
 
15. session 15 data binding
15. session 15   data binding15. session 15   data binding
15. session 15 data binding
 
01. session 01 introduction to html
01. session 01   introduction to html01. session 01   introduction to html
01. session 01 introduction to html
 

Recently uploaded

GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

10. session 10 loops and arrays

  • 1. LOOPS AND ARRAYS Session 10
  • 2. Objectives  Describe while loop.  Explain for loop.  Describe do-while loop.  Explain break and continue statements.  Describe the methods of Array object.
  • 3. What is a loop?  Loop is a section of code in a program which is executed repeatedly, until a specific condition is satisfied.  There are three type of loop structures: The while loop The do-while loop The for loop
  • 4. The while loop  The while loop executes a block of code as long as the given condition remains Conditio false true. n  Syntax: true while (condition) { Execute body Exit loop statement(s); of loop }
  • 5. The while loop: Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>while loop demo</title> <script language="javascript" type="text/javascript"> var n=20; var i=0; document.write("Display the odd number <br>"); while(i<=n) { if(i%2 == 0) document.write(i + "t"); i++; } </script> </head> <body> </body> </html>
  • 6. The do-while loop  The do-while loop is similar to the while loop, but the do-while loop do evaluates the condition at the end of the loop. So that, the do-while loop Execute body executes at least once. of loop  Syntax: do true Conditio n { statement(s); false }while(condition); Exit loop
  • 7. The do-while loop: Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>while loop demo</title> <script language="javascript" type="text/javascript"> var n=20; var i=0; document.write("Display the even number <br>"); do { if(i%2 == 0) document.write(i + "t"); i++; } while(i<=n); </script> </head> <body> </body> </html>
  • 8. The for loop  Syntax: for(initialization; condition; increment/decrement) { statement(s); }  The initialization is an assignment statement that sets the loop control variable, before entering the loop.  The condition is a relational expression, which determines, when the loop will exit.  The increment/decrement defines how the loop control variable changes, each time the loop is executed.
  • 9. The for loop: Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>for loop demo</title> <script language="javascript" type="text/javascript"> var n=20; var i=0; document.write("Display the even number <br>"); for(i=0; i<n; i++) { if(i%2 == 0) document.write(i + "t"); } </script> </head> <body> </body> </html>
  • 10. break statement  The break statement can be used in the switch- case and loop constructs. It is used to exit the loop without evaluating the specified condition. for(initialization; condition; increment/decrement) { …. if(condition) break; … } ...
  • 11. break statement: Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Using break statement</title> <script language="javascript" type="text/javascript"> var n = prompt("Enter the number n:"); var i=0; for(i=2; i<n; i++) { if(n % i == 0) { document.write(n + " is not a prime number."); break; } } if(i == n) document.write(n + " is prime number."); </script> </head> </html>
  • 12. continue statement  The continue statement is mostly used in the loop constructs. It is used to terminate the current execution of the loop and continue with the next repetition by returning the control to the beginning of the loop. for(initialization; condition; increment/decrement) { …. if(condition) continue; … } ...
  • 13. continue statement: Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>continue statement demo</title> <script language="javascript" type="text/javascript"> var n=20; var i=0; document.write("Display the even numbers <br>"); for(i=0; i<=n; i++) { if(i%2 == 1) continue; else document.write(i + "t"); } </script> </head> </html>
  • 14. Single-dimensional arrays  An array is a collection of values stored in adjacent memory locations. These array values are referenced using a common array name.  The values of array are the same data type. These values can be accessed by using the subscript or index numbers. The index determines the position of element in the array list.  In a single-dimensional array, the elements are stored in a single row in the located memory.  In JavaScript, the first element has the index number zero. Index Value
  • 15. Declaring arrays There are three ways to declaring an array:  Using the Array object: declare an array by using the new operator, and then initialize the individual array elements: var arr = new Array(3); arr[0] = “Single”; arr[1] = “Married”; arr[2] = “Divorced”;  Initialize the array variable at the time of declaration: var arr = new Array(„Single‟,‟Married‟,‟Divorced‟);  Creates an array without using Array object: var arr = {„Single‟,‟Married‟,‟Divorced‟};
  • 16. Single-dimensional arrays - Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Single-dimensional array</title> <script language="javascript" type="text/javascript"> var arr = new Array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi","Dimanche"); document.write("The days in a week <br>"); for(i=0; i<arr.length; i++) { document.write(arr[i] + '<BR>'); } </script> </head> </html>
  • 17. Multi-dimensional arrays  A multi-dimensional array stores a combination of values of a single type in two or more dimensions.  A two-dimensional array represents as rows and columns similar to a table.  JavaScript does not directly support two-dimensional array. You can create two-dimensional array by creating an array of arrays. You first declare an array and then create another array to each element of the main array. var emp= new Array(3); emp[0] = new Array(„John‟,‟300‟); emp[1] = new Array(„David‟,‟400‟); emp[2] = new Array(„Richard‟,‟500‟);
  • 18. Multi-dimensional arrays - Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Single-dimensional array</title> <script language="javascript" type="text/javascript"> var emp = new Array(3); emp[0] = new Array('Huynh Bay','1900'); emp[1] = new Array('Minh Thanh','900'); emp[2] = new Array('Tan Hung','800'); document.write('<table border=1> <tr> <th>Name</th> <th>Salary</th> </tr>'); for(var i=0; i<emp.length; i++) { document.write('<tr>'); for(var j=0; j<emp[i].length; j++) document.write('<td>' + emp[i][j] + '</td>'); document.write('</tr>'); } </script> </head> </html>
  • 19. Array methods  An array is a set of values grouped together an identified by a single name. In JavaScript, an Array allows you to create arrays. It provides the length property that is used to determine the number of elements in an array.  The various methods allow you to access and manipulate the array elements: - concat: combines one or more array variables. - join: joins all the array elements into a string. - pop: retrieves the last element of an array. - push: appends one or more elements to the end of an array. - sort: sorts the array elements in alphabetical order. - reverse: retrieves the elements from the last to the first index position. - toString: converts the array elements into string.
  • 20. Array methods - Demo <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Using method in array</title> <script language="javascript" type="text/javascript"> var days = new Array('Monday','Tuesday','Wednesday'); document.write('Number of days: ' + days.length + '<br>'); document.write('The days: ' + days.join(', ') + '<br>'); document.write('The days after adding more: ' + days.push('Thursday','Friday','Saturday') + '<br>'); document.write('The days after sorting:' + days.sort() + '<br>'); document.write('The days after sorting in desceding order:' + days.reverse() + '<br>'); </script> </head> </html>
  • 21. for … in Loop  The for…in loop is an extension of for loop. It enables you to perform specific actions on the array objects. The loop reads every element in the specified array and executes a block of code once for each element in the array.  Example: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Using method in array</title> <script language="javascript" type="text/javascript"> var arr = new Array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi","Dimanche"); document.write('THE DAYS IN A WEEK: <BR>'); for(var i in arr) { document.write(arr[i] + '<br>'); } </script> </head> </html>
  • 22. Summary  Loop statements allow to execute the same block of code multiple times depending whether the specified condition is sastified or not.  Array is a collection of values of the same type.  Javascript support three types of loop:  While loop  For loop  Do….while loop  Jump statements: break, continue Building Dynamic Websites/Session 1/ 22 of 16
  • 23. Summary…  There are two types arrays:  Single dimensional arrays  Multiple dimensional arrays  For….in loop is an extension of the for loop. Building Dynamic Websites/Session 1/ 23 of 16