Chapter 3 - Arrays - ControllingFlow
Chapter 3 - Arrays - ControllingFlow
Sixth Edition
Chapter 3
Building Arrays and Controlling Flow
2
Objectives
Array
Set of data represented by a single variable name
Array literal
most common way to create an array
declares a variable and specifies array as content
Syntax
var name = [value1, value2, value3, …];
Example:
Create an array named newsSections containing 4 strings as
elements
Element
Each piece of data contained in an array
Index
Element’s numeric position within the array
Array element numbering
Starts with index number of zero (0)
Reference element using its index number
Example: to reference the 2nd element in the newsSections array
newsSections[1]
newsSections[4] = "entertainment";
colors[2] = "yellow";
newsSections[4] = "living";
length property
Returns the number of elements in an array
Syntax
name.length;
getElementsByTagName() method
Can reference web page element by looking up all elements of a
certain type in document and referencing one element in that
collection
Resulting collection uses syntax similar to arrays
Example:
document.getElementsByTagName("li")[2]
Loop statement
Control flow statement repeatedly executing a statement or a series
of statements
While a specific condition is true or until a specific condition becomes
true
Three types of loop statements
while statements
do/while statements
for statements
while statement
Repeats a statement or series of statements
As long as a given conditional expression evaluates to a truthy value
Syntax
while (expression) {
statements
}
Iteration
Each repetition of a looping statement
Counter
Variable incremented or decremented with each loop statement
iteration
Examples:
while statement using an increment operator
while statement using a decrement operator
while statement using the *= assignment operator
var count = 1;
while (count <= 5) {
document.write(count + "<br />");
count++;
}
document.write("<p>You have printed 5 numbers.</p>");
Result in browser:
Result in browser:
var count = 1;
while (count <= 100) {
document.write(count + "<br />");
count *= 2;
}
Result in browser:
Infinite loop
Loop statement that never ends
Conditional expression: never false
Example:
var count = 1;
while (count <= 10) {
window.alert("The number is " + count + ".");
}
Example:
assigning array element values to table cells:
function addColumnHeaders()
{
var i = 0;
while (i < 7)
{
document.getElementsByTagName("th")↵
[i].innerHTML = daysOfWeek[i];
i++;
}
}
21
do/while Statements
do/while statement
Executes a statement or statements once
Then repeats the execution as long as a given conditional expression evaluates to a truthy
value
Syntax
do {
statements;
} while (expression);
Examples:
var count = 2;
do {
document.write("<p>The count is equal to " +↵
count + ".</p>");
count++;
} while (count < 2);
var count = 2;
while (count < 2) {
document.write("<p>The count is equal to " +↵
count + ".</p>");
count++;
}
JavaScript, Sixth Edition
23
do/while Statements (cont’d.)
Example:
adding days of week with a do/while statement instead of a while statement
var i = 0;
do {
document.getElementsByTagName("th")[i].innerHTML =↵
daysOfWeek[i];
i++;
} while (i < 7);
for statement
Repeats a statement or series of statements
As long as a given conditional expression evaluates to a truthy value
Can also include code that initializes a counter and changes its value with each iteration
Syntax
Result in browser:
for statement
More efficient than a while statement
Examples:
var count = 1;
while (count < brightestStars.length) {
document.write(count + "<br />");
count++;
}
for (var count = 1; count < brightestStars.length; count++) {
document.write(count + "<br />");
}
JavaScript, Sixth Edition
28
for Statements (cont’d.)
Example:
addColumnHeaders() function with a for statement instead of a do/while
statement
function addColumnHeaders()
{
for (var i = 0; i < 7; i++)
{
document.getElementsByTagName("th")[i].innerHTML = daysOfWeek[i];
}
}
29
Using continue Statements to
Restart Execution
continue statement
Halts a looping statement
Restarts the loop with a new iteration
Used to stop a loop for the current iteration
Have the loop to continue with a new iteration
Examples:
for loop with a continue statement
Result in browser:
Decision making
Process of determining the order in which statements execute in a
program
Decision-making statements, decision-making structures, or
conditional statements
Special types of JavaScript statements used for making decisions
if statement
Most common type of decision-making statement
if (condition)
{
statements
}
After the if statement executes:
Any subsequent code executes normally
if (expression)
{
statements
}
else {
statements
}
JavaScript, Sixth Edition
35
if/else Statements (cont’d.)
Example:
if (submitButton.addEventListener)
{
submitButton.addEventListener("click", submitForm, false);
}
else if (submitButton.attachEvent)
{
submitButton.attachEvent("onclick", submitForm);
}
JavaScript, Sixth Edition
41
switch Statements
Syntax
switch (expression)
{
case label:
statements;
break;193
case label:
statements;
break;
...
default:
statements;
break;
}
43
switch Statements (cont’d.)
default label
Executes when the value returned by the switch statement
expression does not match a case label
When a switch statement executes:
Value returned by the expression is compared to each case label
In the order in which it is encountered
break statement
Ends execution of a switch statement
Should be final statement after each case label
function city_location(americanCity)
{
switch (americanCity)
{
case "Boston":
return "Massachusetts";
break;
case "Chicago":
return "Illinois";
break;
case "Los Angeles":
return "California";
break;
case "Miami":
return "Florida";
break;
case "New York":
return "New York";
break;
default:
return "United States";
break;
}
}
document.write("<p>" + city_location("Boston") + "</p>");
45
Summary
Array
Set of data represented by a single variable name
Index: element’s numeric position within the array
Can access and modify array elements
length property
number of elements in an array
Loop statements
while statements, do/while statements, and for statements
Iteration: each repetition of a looping statement
Counter: variable
Incremented or decremented with each iteration of a loop statement
continue statement
Restarts a loop with a new iteration
Decision making
Determining the order in which statements execute in a program
May execute in a linear fashion
if statement,if/else statement, else if construction
Nested decision-making structures
switch statement and case labels
break statement: used to exit control statements
Command block
Set of statements contained within a set of braces
May repeat the same statement, function, or code section