10. session 10 loops and arrays

23
LOOPS AND ARRAYS Session 10

Upload: phuc-do

Post on 10-Dec-2014

1.090 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 10. session 10   loops and arrays

LOOPS AND ARRAYS

Session 10

Page 2: 10. session 10   loops and arrays

Objectives

Describe while loop. Explain for loop. Describe do-while loop. Explain break and continue statements. Describe the methods of Array object.

Page 3: 10. session 10   loops and arrays

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

Page 4: 10. session 10   loops and arrays

The while loop The while loop executes

a block of code as long as the given condition remains true.

Syntax:while (condition){statement(s);

}

Condition

Execute body

of loopExit loop

false

true

Page 5: 10. session 10   loops and arrays

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>

Page 6: 10. session 10   loops and arrays

The do-while loop The do-while loop is similar to

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

Syntax:do{statement(s);

}while(condition);

Condition

Execute body

of loop

Exit loop

false

true

do

Page 7: 10. session 10   loops and arrays

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>

Page 8: 10. session 10   loops and arrays

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.

Page 9: 10. session 10   loops and arrays

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>

Page 10: 10. session 10   loops and arrays

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; …}...

Page 11: 10. session 10   loops and arrays

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>

Page 12: 10. session 10   loops and arrays

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; …}

...

Page 13: 10. session 10   loops and arrays

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;

elsedocument.write(i + "\t");

}</script></head>

</html>

Page 14: 10. session 10   loops and arrays

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.

ValueIndex

Page 15: 10. session 10   loops and arrays

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’};

Page 16: 10. session 10   loops and arrays

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>

Page 17: 10. session 10   loops and arrays

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’);

Page 18: 10. session 10   loops and arrays

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>

Page 19: 10. session 10   loops and arrays

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.

Page 20: 10. session 10   loops and arrays

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>

Page 21: 10. session 10   loops and arrays

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>

Page 22: 10. session 10   loops and arrays

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, continueBuilding Dynamic Websites/Session 1/ 22 of 16

Page 23: 10. session 10   loops and arrays

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