LOOPS AND ARRAYS       Session 10
Objectives   Describe while loop.   Explain for loop.   Describe do-while loop.   Explain break and continue statement...
What is a loop?   Loop is a section of code in a program which is    executed repeatedly, until a specific condition is  ...
The while loop   The while loop executes a    block of code as long as the    given condition remains        Conditio    ...
The while loop: Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>while loop demo</title>   <script langua...
The do-while loop   The do-while loop is similar to the    while loop, but the do-while loop                 do    evalua...
The do-while loop: Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>while loop demo</title>   <script lan...
The for loop Syntax:for(initialization; condition; increment/decrement){  statement(s);}   The initialization is an assi...
The for loop: Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>for loop demo</title>   <script language="...
break statement   The break statement can be used in the switch-    case and loop constructs. It is used to exit the    l...
break statement: Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>Using break statement</title>   <script...
continue statement   The continue statement is mostly used in the loop    constructs. It is used to terminate the current...
continue statement: Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>continue statement demo</title>   <s...
Single-dimensional arrays   An array is a collection of values stored in adjacent    memory locations. These array values...
Declaring arraysThere are three ways to declaring an array: Using the Array object: declare an array by using the new  op...
Single-dimensional arrays - Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>Single-dimensional array</ti...
Multi-dimensional arrays   A multi-dimensional array stores a combination of values of    a single type in two or more di...
Multi-dimensional arrays - Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>Single-dimensional array</tit...
Array methods   An array is a set of values grouped together an identified by    a single name. In JavaScript, an Array a...
Array methods - Demo<html xmlns="http://www.w3.org/1999/xhtml">   <head>   <title>Using method in array</title>         <s...
for … in Loop   The for…in loop is an extension of for loop. It enables you    to perform specific actions on the array o...
Summary   Loop statements allow to execute the same block    of code multiple times depending whether the    specified co...
Summary…   There are two types arrays:       Single dimensional arrays       Multiple dimensional arrays   For….in loo...
Upcoming SlideShare
Loading in …5
×

10. session 10 loops and arrays

1,141 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,141
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

10. session 10 loops and arrays

  1. 1. LOOPS AND ARRAYS Session 10
  2. 2. Objectives Describe while loop. Explain for loop. Describe do-while loop. Explain break and continue statements. Describe the methods of Array object.
  3. 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. 4. The while loop The while loop executes a block of code as long as the given condition remains Conditio false true. n Syntax: truewhile (condition){ Execute body Exit loop statement(s); of loop}
  5. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 15. Declaring arraysThere 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. 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. 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. 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. 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. 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. 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. 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. 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

×