Announcements Section Voting is up
Vote in your section by Thursday, 10pm Assignment: 20 pts for voting
Two labs this week: Web App Design Lab Coin Flipping Lab
You decide the order that you do them
2012-05-07 Katherine Deibel, Fluency in Information Technology 1
Arrays & IterationGoing beyond one
Fluency with Information Technology
INFO100 and CSE100
Katherine Deibel
2012-05-07 Katherine Deibel, Fluency in Information Technology 2
Learning Programming Programmer Braggery:
How many languages do you know? The answer really does not matter
Many languages are regional dialects of each other (share syntax and concepts) Javascript, Java, C#, and Visual Basic are all
object-oriented languages Scottish accent, Southeast U.S. accent, Boston
accent, London accent
2012-05-07 Katherine Deibel, Fluency in Information Technology 3
When faced with a new language…
I look for how to do the following: Declare variables If-Else statements Make and use functions Manipulate strings Loops and iteration Arrays
2012-05-07 Katherine Deibel, Fluency in Information Technology 4
Labs 7 & 8 and Project 2
Today's lecture
The Takeaway:You have learned the basics for working in many different programming languages.
Katherine Deibel, Fluency in Information Technology 5
Definitions Iteration, or looping, is the process of
repetition: looping through a sequence of
statements to repeat them
2012-05-07
Major Types of Repetitions For loop
Run a fixed number of times While loop
Run 0+ times until condition is met Do while loop
Run 1+ times until condition is met
2012-05-07 Katherine Deibel, Fluency in Information Technology 6
For LoopsDo I need to repeat myself?
2012-05-07 Katherine Deibel, Fluency in Information Technology 7
For Loop Basic Syntax
for (<initial>; <condition>; <next>){
<statement list>} Program completes the entire
statement sequence of the <statement list> during each iteration
2012-05-07 Katherine Deibel, Fluency in Information Technology 8
Control Specification The three operations in the
parentheses of the for loop Control the number of times the loop
iterates by using an iteration variable (must be
declared)
2012-05-07 Katherine Deibel, Fluency in Information Technology 9
Katherine Deibel, Fluency in Information Technology 10
How a For Loop Works Consider a computation on declared
variables j and text
2012-05-07
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
How a For Loop Works Consider a computation on declared
variables j and text
control specification
2012-05-07 Katherine Deibel, Fluency in Information Technology 11
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
How a For Loop Works Consider a computation on declared
variables j and text
initialization
2012-05-07 Katherine Deibel, Fluency in Information Technology 12
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
How a For Loop Works Consider a computation on declared
variables j and text
continuation condition
2012-05-07 Katherine Deibel, Fluency in Information Technology 13
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
How a For Loop Works Consider a computation on declared
variables j and text
step size or increment
2012-05-07 Katherine Deibel, Fluency in Information Technology 14
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
How a For Loop Works Consider a computation on declared
variables j and text
NO SEMICOLONS!!
2012-05-07 Katherine Deibel, Fluency in Information Technology 15
Processing for Loops
Step One: <initialization> Sets (and maybe declares) the iteration
variable's value for the first iteration of the loop
Initialization is done only once Example: j is declared and set to 1
2012-05-07 Katherine Deibel, Fluency in Information Technology 16
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) {
text = text + "Never! ";}alert(text);
Processing for Loops
Step Two: <continuation> The <continuation condition> is tested
If true, the statement list is computed. If false, the <statement list> is skipped and
control passes to the statement after the for loop Example: j <= 3 and text = …
2012-05-07 Katherine Deibel, Fluency in Information Technology 17
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) {
text = text + "Never! ";}alert(text);
Processing for Loops
Step Three: <next iteration> The <next iteration> statement is
computed Loop returns to Step Two Example: j = j + 1
2012-05-07 Katherine Deibel, Fluency in Information Technology 18
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) {
text = text + "Never! ";}alert(text);
Katherine Deibel, Fluency in Information Technology 19
Example
2012-05-07
text = "She said ";for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! ";}alert(text);
When j j <= 3 text j++Before loop - - "She said "First iteration 1 true "She said Never! " 2Next iteration 2 true "She said Never! Never! " 3
Next iteration 3 true "She said Never! Never! Never! " 4Next iteration 4 false - -After loop text == "She said Never! Never! Never! "
A World-Famous Iteration
Most frequently written for loop Easy to see iteration count: Always runs n times Goes from 0 to n-1
2012-05-07 Katherine Deibel, Fluency in Information Technology 20
for ( var i = 0; i < n; i++ ) {…}
For Loop Syntax: Initialization
The Iteration Variable Must be declared in the loop or in the
code before the loop Must follow rules for variable identifiers i, j, and k are the most common choices
The Starting Point Iteration can begin anywhere, including
negative numbers
2012-05-07 Katherine Deibel, Fluency in Information Technology 21
For Loop Syntax: Continuation
Continuation/Termination Test Test is any expression resulting in a
Boolean value (true/false) Continuation must involve iteration
variable to avoid infinite loop
2012-05-07 Katherine Deibel, Fluency in Information Technology 22
Katherine Deibel, Fluency in Information Technology 23
For Loop Syntax: Step Size The amount of change in the iteration
variable from one iteration to the next Often called the increment or decrement
Increment: j = j + 1 Decrement: j = j – 1
Often uses shorthand: j++ or j-- Can be any size
j = j + 5 j -= 2
2012-05-07
Nested for Loop Syntaxfor (<initial i>; <condition i>; <next i>) {
<some statements>for (<initial j>; <condition j>; <next j>) {
<some statements>}<some statements>
}
2012-05-07 Katherine Deibel, Fluency in Information Technology 24
Three-Level Nested For Loop
2012-05-07 Katherine Deibel, Fluency in Information Technology 25
for( var i = 1; i <= N_i; i++ ) {
for( var j = 1; j <= N_j; j++ ) {
for( var k = 1; k <= N_k; k++ ) {
}}
}
Syntax vs Logic Errors
There are two ways to go wrong when programming Syntax Errors how the code is written Logic Errors what the code does
Remember: Just writing code that runs does not mean it runs correctly
2012-05-07 Katherine Deibel, Fluency in Information Technology 26
for (var j = 5; j > 0; j++) ;{ //statement body}
Syntactically correct codeRuns forever (BAD!)
Complex Loop Conditions Loop conditions can be fairly complex
The above loop will run 8 times unless total becomes zero or negative first
2012-05-07 Katherine Deibel, Fluency in Information Technology 27
for (var j = 0; j < 8 && total > 0; j++){ //statement body}
Arrays and IndexesStoring a Collection of Items
2012-05-07 Katherine Deibel, Fluency in Information Technology 28
Katherine Deibel, Fluency in Information Technology 29
What is an Array? An indexed list of items Indexed means each element in the
list has a number, or index
2012-05-07
What is an Array?16. Abraham Lincoln17. Andrew Johnson18. Ulysses S. Grant19. Rutherford B Hayes20. James Garfield21. Chester Arthur22. Grover Cleveland23. Benjamin Harrison24. Grover Cleveland25. William McKinley26. Theodore Roosevelt27. William H. Taft28. Woodrow Wilson29. Warren Harding30. Calvin Coolidge
31. Herbert Hoover32. Franklin D.
Roosevelt33. Harry S. Truman34. Dwight Eisenhower35. John Kennedy36. Lyndon Johnson37. Richard Nixon38. Gerald Ford39. James Carter40. Ronald Reagan41. George H. W. Bush42. William Clinton43. George W. Bush44. Barack Obama
2012-05-07 Katherine Deibel, Fluency in Information Technology 30
1. George Washington2. John Adams3. Thomas Jefferson4. James Madison5. James Monroe6. John Quincy Adams7. Andrew Jackson8. Martin Van Buren9. William Harrison10. John Tyler11. James Polk12. Zachary Taylor13. Millard Fillmore14. Franklin Pierce15. James Buchanan
Katherine Deibel, Fluency in Information Technology 31
Indexing Process of creating a sequence of names by
associating a base name with a number (like Apollo 13 or Henry VIII)
Each indexed item is called an element of the base-named sequence
2012-05-07
Indexing and Iteration Index Syntax
Index number is enclosed in square brackets [ ]
Iterations can be used to refer to all elements of a name A[j] for successive iterations over j
referring to different elements of A
2012-05-07 Katherine Deibel, Fluency in Information Technology 32
Katherine Deibel, Fluency in Information Technology 33
Where is the first element? Index Origin
The point at which indexing begins (the least index)
First element In life, the first element may begin with
1, or have no number (Queen Elizabeth) JavaScript and most programming
languages always uses index origin 0
2012-05-07
Katherine Deibel, Fluency in Information Technology 34
JS Syntax for Arrays with name and # elements
var books = new Array(6); with name and elements
var shapes = new
Array("square","circle","triangle");
2012-05-07
Katherine Deibel, Fluency in Information Technology 35
JS Syntax for Arrays (cont.) Accessing elements in array
shapes[1] is "circle" Changing element in array
shapes[0] = "rectangle"; Adding more elements to the array
shapes[3] = "ellipse";shapes[4] = "heptagon"
orshapes.push("diamond"); adds to the end of the array
2012-05-07
Katherine Deibel, Fluency in Information Technology 36
Referring to an Array Element
Referencing an element of the array:shapes[<index>]
Index must be a non-negative integer or expression or variable that resolves to non-negative integer
shapes[1] = …i=3;shapes[i] = …shapes[i+2] = …
2012-05-07
Katherine Deibel, Fluency in Information Technology 37
Iteration and Arrays array.length returns the highest
index in the array (the number of elements in it)
2012-05-07
for( var i=0; i<shapes.length; i++){
}
Katherine Deibel, Fluency in Information Technology 38
Iteration and Arrays
2012-05-07
var i, text=""; /*declare iteration and other variables*/var fruits = new Array(
'lemons','apples','mangoes','tangerines','kumquats','cantaloupe','peaches','grapefruit','raspberries');
alert("Total number of fruits is " + fruits.length);
for (i=0; i<fruits.length; i++){ text += i + '. ' + fruits[i] + '<br />';}
document.write("<h1>Elements of Fruits Array:</h1><p>" + text + "</p>");
While & Do While LoopsMore looping… <nausea>
2012-05-07 Katherine Deibel, Fluency in Information Technology 39
Uncertain number of repeats For loops are good when a fixed number of
repetitions is needed The number of repetitions may not be
known a priori Example: Weight limits on an elevator
Get weight of next person in line If weight plus total so far is less than limit, that
person gets on Once limit is passed, last person gets off and
elevator goes up2012-05-07 Katherine Deibel, Fluency in Information Technology 40
Example While Loop
2012-05-07 Katherine Deibel, Fluency in Information Technology 41
/* weights is an array of people's weights limit is the elevator's weight limit */var currWeight = 0;var j = 0;while (weights[j] + currWeight <= limit){
currWeight = currWeight + weights[j];j = j + 1;
}
Do While Loops In the previous example, the loop would
not run if the first person exceeded the weight limit
While loops can run zero or more times Do while loops always run at least once
2012-05-07 Katherine Deibel, Fluency in Information Technology 42
do {<statements>
} while (condition);
A note about semicolons Semicolons do NOT go after the loop
statement for for loops and while loops
2012-05-07 Katherine Deibel, Fluency in Information Technology 43
do {<statements>
} while (condition);
for( var i=0; i<n; i++){}
while(x<=maximum){}
There IS a semicolon after the loop statement for a do while loop.
Summary Loops allow us to repeat steps Arrays are a means for working with
multiple values These are the last of the essential
aspects to programming
2012-05-07 Katherine Deibel, Fluency in Information Technology 44