Presentation is loading. Please wait.

Presentation is loading. Please wait.

 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.

Similar presentations


Presentation on theme: " 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."— Presentation transcript:

1  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-07Katherine Deibel, Fluency in Information Technology1

2 Fluency with Information Technology INFO100 and CSE100 Katherine Deibel 2012-05-07Katherine Deibel, Fluency in Information Technology2

3  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-07Katherine Deibel, Fluency in Information Technology3

4  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-07Katherine Deibel, Fluency in Information Technology4  Labs 7 & 8 and Project 2  Today's lecture The Takeaway: You have learned the basics for working in many different programming languages.

5  Iteration, or looping, is the process of repetition:  looping through a sequence of statements to repeat them 2012-05-07Katherine Deibel, Fluency in Information Technology5

6  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-07Katherine Deibel, Fluency in Information Technology6

7 Do I need to repeat myself? 2012-05-07Katherine Deibel, Fluency in Information Technology7

8 for ( ; ; ) { }  Program completes the entire statement sequence of the during each iteration 2012-05-07Katherine Deibel, Fluency in Information Technology8

9  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-07Katherine Deibel, Fluency in Information Technology9

10  Consider a computation on declared variables j and text 2012-05-07Katherine Deibel, Fluency in Information Technology10 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);

11 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);  Consider a computation on declared variables j and text control specification 2012-05-07Katherine Deibel, Fluency in Information Technology11

12 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);  Consider a computation on declared variables j and text initialization 2012-05-07Katherine Deibel, Fluency in Information Technology12

13 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);  Consider a computation on declared variables j and text continuation condition 2012-05-07Katherine Deibel, Fluency in Information Technology13

14 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);  Consider a computation on declared variables j and text step size or increment 2012-05-07Katherine Deibel, Fluency in Information Technology14

15 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);  Consider a computation on declared variables j and text NO SEMICOLONS!! 2012-05-07Katherine Deibel, Fluency in Information Technology15

16 Step One:  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-07Katherine Deibel, Fluency in Information Technology16 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);

17 Step Two:  The is tested  If true, the statement list is computed.  If false, the is skipped and control passes to the statement after the for loop  Example: j <= 3 and text = … 2012-05-07Katherine Deibel, Fluency in Information Technology17 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);

18 Step Three:  The statement is computed  Loop returns to Step Two  Example: j = j + 1 2012-05-07Katherine Deibel, Fluency in Information Technology18 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text);

19 2012-05-07Katherine Deibel, Fluency in Information Technology19 text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text); Whenjj <= 3textj++ Before loop--"She said " First iteration1true"She said Never! "2 Next iteration2true"She said Never! Never! "3 Next iteration3true"She said Never! Never! Never! "4 Next iteration4false-- After looptext == "She said Never! Never! Never! "

20  Most frequently written for loop  Easy to see iteration count:  Always runs n times  Goes from 0 to n-1 2012-05-07Katherine Deibel, Fluency in Information Technology20 for ( var i = 0; i < n; i++ ) {…}

21  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-07Katherine Deibel, Fluency in Information Technology21

22  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-07Katherine Deibel, Fluency in Information Technology22

23  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-07Katherine Deibel, Fluency in Information Technology23

24 for ( ; ; ) { for ( ; ; ) { } 2012-05-07Katherine Deibel, Fluency in Information Technology24

25 2012-05-07Katherine Deibel, Fluency in Information Technology25 for( var i = 1; i <= N_i; i++ ) { for( var j = 1; j <= N_j; j++ ) { for( var k = 1; k <= N_k; k++ ) { }

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

27  Loop conditions can be fairly complex  The above loop will run 8 times unless total becomes zero or negative first 2012-05-07Katherine Deibel, Fluency in Information Technology27 for (var j = 0; j 0; j++){ //statement body }

28 Storing a Collection of Items 2012-05-07Katherine Deibel, Fluency in Information Technology28

29  An indexed list of items  Indexed means each element in the list has a number, or index 2012-05-07Katherine Deibel, Fluency in Information Technology29

30 16. Abraham Lincoln 17. Andrew Johnson 18. Ulysses S. Grant 19. Rutherford B Hayes 20. James Garfield 21. Chester Arthur 22. Grover Cleveland 23. Benjamin Harrison 24. Grover Cleveland 25. William McKinley 26. Theodore Roosevelt 27. William H. Taft 28. Woodrow Wilson 29. Warren Harding 30. Calvin Coolidge 31. Herbert Hoover 32. Franklin D. Roosevelt 33. Harry S. Truman 34. Dwight Eisenhower 35. John Kennedy 36. Lyndon Johnson 37. Richard Nixon 38. Gerald Ford 39. James Carter 40. Ronald Reagan 41. George H. W. Bush 42. William Clinton 43. George W. Bush 44. Barack Obama 2012-05-07Katherine Deibel, Fluency in Information Technology30 1. George Washington 2. John Adams 3. Thomas Jefferson 4. James Madison 5. James Monroe 6. John Quincy Adams 7. Andrew Jackson 8. Martin Van Buren 9. William Harrison 10. John Tyler 11. James Polk 12. Zachary Taylor 13. Millard Fillmore 14. Franklin Pierce 15. James Buchanan

31  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-07Katherine Deibel, Fluency in Information Technology31

32  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-07Katherine Deibel, Fluency in Information Technology32

33  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-07Katherine Deibel, Fluency in Information Technology33

34  with name and # elements var books = new Array(6);  with name and elements var shapes = new Array("square","circle","triangle"); 2012-05-07Katherine Deibel, Fluency in Information Technology34

35  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" or shapes.push("diamond"); adds to the end of the array 2012-05-07Katherine Deibel, Fluency in Information Technology35

36  Referencing an element of the array: shapes[ ]  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-07Katherine Deibel, Fluency in Information Technology36

37  array.length returns the highest index in the array (the number of elements in it) 2012-05-07Katherine Deibel, Fluency in Information Technology37 for( var i=0; i<shapes.length; i++) { }

38 2012-05-07Katherine Deibel, Fluency in Information Technology38 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] + ' '; } document.write(" Elements of Fruits Array: " + text + " ");

39 More looping… 2012-05-07Katherine Deibel, Fluency in Information Technology39

40  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 2012-05-07Katherine Deibel, Fluency in Information Technology40

41 2012-05-07Katherine Deibel, Fluency in Information Technology41 /*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; }

42  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-07Katherine Deibel, Fluency in Information Technology42 do { } while (condition);

43  Semicolons do NOT go after the loop statement for for loops and while loops 2012-05-07Katherine Deibel, Fluency in Information Technology43 do { } 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.

44  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-07Katherine Deibel, Fluency in Information Technology44


Download ppt " 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."

Similar presentations


Ads by Google