1 / 44

Announcements

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. INFO100 and CSE100. Fluency with Information Technology.

santo
Télécharger la présentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Katherine Deibel, Fluency in Information Technology

  2. INFO100 and CSE100 Fluency with Information Technology Arrays & IterationGoing beyond one Katherine Deibel Katherine Deibel, Fluency in Information Technology

  3. 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 Katherine Deibel, Fluency in Information Technology

  4. 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  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 Katherine Deibel, Fluency in Information Technology

  6. 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 Katherine Deibel, Fluency in Information Technology

  7. For Loops Do I need to repeat myself? Katherine Deibel, Fluency in Information Technology

  8. For Loop Basic Syntax for (<initial>; <condition>; <next>){ <statement list>} • Program completes the entire statement sequence of the <statement list> during each iteration Katherine Deibel, Fluency in Information Technology

  9. 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) Katherine Deibel, Fluency in Information Technology

  10. How a For Loop Works • Consider a computation on declared variables j and text • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) • { • text = text + "Never! "; • } • alert(text); Katherine Deibel, Fluency in Information Technology

  11. How a For Loop Works • Consider a computation on declared variables j and text • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) • { • text = text + "Never! "; • } • alert(text); control specification Katherine Deibel, Fluency in Information Technology

  12. How a For Loop Works • Consider a computation on declared variables j and text • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) • { • text = text + "Never! "; • } • alert(text); initialization Katherine Deibel, Fluency in Information Technology

  13. How a For Loop Works • Consider a computation on declared variables j and text • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) • { • text = text + "Never! "; • } • alert(text); continuation condition Katherine Deibel, Fluency in Information Technology

  14. How a For Loop Works • Consider a computation on declared variables j and text • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) • { • text = text + "Never! "; • } • alert(text); step size or increment Katherine Deibel, Fluency in Information Technology

  15. How a For Loop Works • Consider a computation on declared variables j and text • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) • { • text = text + "Never! "; • } • alert(text); NO SEMICOLONS!! Katherine Deibel, Fluency in Information Technology

  16. Processing for Loops • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) { • text = text + "Never! "; • } • alert(text); 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 Katherine Deibel, Fluency in Information Technology

  17. Processing for Loops • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) { • text = text + "Never! "; • } • alert(text); 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 = … Katherine Deibel, Fluency in Information Technology

  18. Processing for Loops • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) { • text = text + "Never! "; • } • alert(text); Step Three: <next iteration> • The <next iteration> statement is computed • Loop returns to Step Two • Example: j = j + 1 Katherine Deibel, Fluency in Information Technology

  19. Example • text = "She said "; • for ( var j = 1; j <= 3; j = j + 1 ) { • text = text + "Never! "; • } • alert(text); Katherine Deibel, Fluency in Information Technology

  20. A World-Famous Iteration • for ( vari= 0; i< n; i++ ) {…} • Most frequently written for loop • Easy to see iteration count: • Always runs n times • Goes from 0 to n-1 Katherine Deibel, Fluency in Information Technology

  21. 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 Katherine Deibel, Fluency in Information Technology

  22. 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 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 Katherine Deibel, Fluency in Information Technology

  24. Nested for Loop Syntax for (<initial i>; <condition i>; <next i>) { <some statements> for (<initial j>; <condition j>; <next j>) { <some statements> } <some statements> } Katherine Deibel, Fluency in Information Technology

  25. Three-Level Nested For Loop • for( vari= 1; i<= N_i; i++ ) • { • for( var j= 1; j<= N_j; j++ ) • { • for( vark = 1; k <= N_k; k++ ) • { • } • } • } Katherine Deibel, Fluency in Information Technology

  26. 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 for (var j = 5; j > 0; j++) ;{ //statement body} • Syntactically correct code • Runs forever (BAD!) Katherine Deibel, Fluency in Information Technology

  27. Complex Loop Conditions • Loop conditions can be fairly complex • The above loop will run 8 times unless total becomes zero or negative first • for (var j = 0; j < 8 && total > 0; j++){ //statement body} Katherine Deibel, Fluency in Information Technology

  28. Arrays and Indexes Storing a Collection of Items 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 Katherine Deibel, Fluency in Information Technology

  30. What is an Array? • George Washington • John Adams • Thomas Jefferson • James Madison • James Monroe • John Quincy Adams • Andrew Jackson • Martin Van Buren • William Harrison • John Tyler • James Polk • Zachary Taylor • Millard Fillmore • Franklin Pierce • James Buchanan • Abraham Lincoln • Andrew Johnson • Ulysses S. Grant • Rutherford B Hayes • James Garfield • Chester Arthur • Grover Cleveland • Benjamin Harrison • Grover Cleveland • William McKinley • Theodore Roosevelt • William H. Taft • Woodrow Wilson • Warren Harding • Calvin Coolidge • Herbert Hoover • Franklin D. Roosevelt • Harry S. Truman • Dwight Eisenhower • John Kennedy • Lyndon Johnson • Richard Nixon • Gerald Ford • James Carter • Ronald Reagan • George H. W. Bush • William Clinton • George W. Bush • Barack Obama 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 Katherine Deibel, Fluency in Information Technology

  32. 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 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 Katherine Deibel, Fluency in Information Technology

  34. JS Syntax for Arrays • with name and # elementsvar books = new Array(6); • with name and elementsvar shapes = new Array("square","circle","triangle"); 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 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] = … 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) • for( vari=0; i<shapes.length; i++) • { • } Katherine Deibel, Fluency in Information Technology

  38. Iteration and Arrays vari, 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>"); Katherine Deibel, Fluency in Information Technology

  39. While & Do While Loops More looping… <nausea> Katherine Deibel, Fluency in Information Technology

  40. 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 up Katherine Deibel, Fluency in Information Technology

  41. Example While Loop • /* weights is an array of people's weights • limit is the elevator's weight limit */ • varcurrWeight = 0; • varj = 0; • while (weights[j] + currWeight <= limit) • { • currWeight = currWeight + weights[j]; • j = j + 1; • } Katherine Deibel, Fluency in Information Technology

  42. 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 • do { • <statements> • } while (condition); Katherine Deibel, Fluency in Information Technology

  43. A note about semicolons • Semicolons do NOT go after the loop statement for for loops and while loops • for( vari=0; i<n; i++) • { • } • while(x<=maximum) • { • } • There IS a semicolon after the loop statement for a do while loop. • do { • <statements> • } while (condition); Katherine Deibel, Fluency in Information Technology

  44. 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 Katherine Deibel, Fluency in Information Technology

More Related