1 / 38

Lecture contents

Lecture contents. Looping while do while for Arrays indexes For each loop. OO. Lecture 7. Going loopy. Looping. Sometimes we want a computer to do something over and over and over and over and over again We can achieve this with a loop

paul
Download Presentation

Lecture contents

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. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  2. OO Lecture 7 Going loopy

  3. Looping • Sometimes we want a computer to do something over and over and over and over and over again • We can achieve this with a loop • a programming structure that performs a series of instructions repeatedly until some specified condition is met

  4. There are different kinds of loops... • while • do while • for • for each (enhanced for) • Recursion (not really a loop)

  5. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  6. While • The fundamental loop • All of the other loops can be built from this

  7. Look at While while(condition){ code to run; } Just like an if statement condition

  8. Look at While int i = 0; while(i<10){ System.out.println(“The number is “+i); i= i+1; } System.out.println(“i is now “ + i); How many times will this loop? What is the value of i when it gets here?

  9. Look at While inti = 0; while(i<10){ System.out.println(“The number is “+i); i= i+1; i++; } System.out.println(“i is now “+i);

  10. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  11. Do while int count = 1; do { System.out.println("Count is: " + count); count++; } while (count <= 11); Always runs at least once

  12. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  13. For loop for (initialization; termination; increment) { statement(s) }

  14. Closer look int i; for ( i =1; i<11; i++ ) { System.out.println(“i is “ + i ); } What is the value of i when it leaves the loop? initialization termination increment

  15. Closer look for ( inti =1; i<11; i++ ) { System.out.println(“i is “ + i ); } System.out.println(“i is now “ = i); You can also declare intiin the for loop If you do this, you will not be able to access i outside of the loop. We come on to this when we cover scope. initialization termination increment

  16. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  17. Arrays • Sometimes we want to group things together • “things” being objects and primitives • The most fundamental collection is an Array • If a variable is a cup..... an array is a shelf of cups [ ] 2 3 4 5 7 1 8 0 6

  18. Details of Arrays • Arrays are objects • They have their own special syntax • To declare an Array: int[] numberStore; or intnumberStore[]; • Both are valid and do the same thing

  19. Details of Arrays • To instantiate an Array: numberStore = new int[10]; This is the length you want your array to be i.e. the number of indexes

  20. Hang on.... numberStore = new int[]; Doesn’t there have to be () – we’re saying new, we’re calling the constructor...? Nope, not for an array, it’s a special case.

  21. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  22. Index An index is the position in the array. Java arrays start at 0 [ ] 2 3 4 5 7 1 8 0 6

  23. Length The length is the number of indexes Here the length is 9 [ ] 2 3 4 5 7 1 8 0 6

  24. Using indexes (insertion) int[] numStore; numStore = new int[9]; numStore[0] = 77; [ ] 2 3 4 5 7 1 8 0 6

  25. Eeek! What’s with the [] again? int[] numStore; numStore = new int[9]; numStore[0] = 77; Declaration Instantiation with length in the [] Assignment to an index with index in the []

  26. Using indexes (insertion) int[] numStore; numStore = new int[9]; numStore[0] = 77; numStore[1] = 45; //and so on [ ] 45 17 54 49 21 8 79 77 80 2 3 4 5 7 1 8 0 6

  27. What is the output of the lines of code on the next slide?

  28. Using indexes (retreval) System.out.println(numStore[0]); num=3; System.out.println(numStore[num]); System.out.println(numStore[num+2]); System.out.println(numStore[num]+3); [ ] 45 17 54 49 21 8 79 77 80 2 3 4 5 7 1 8 0 6

  29. Array rules • [] have three uses: • declaration: int[] numStore • putting numbers in the [] at declaration is illegal in Java • instantiation: numStore = new int[length] • not putting a number in [] at instantiation is illegal in Java • access: To access an element at array index y, you put numStore[y]. You can treat numStore[y] as an int, because that is what it is.

  30. Array rules • An array can hold objects or primitives • An array is an object, regardless of what it contains • Arrays care about type • if you declare an int[] you can only put ints in it* *(not entirely true, look up ‘implicit widening’ if you are interested)

  31. When you play with arrays... • .... you will learn to know and hate the • ArrayIndexOutOfBoundsException • This happens when you try and access an index that isn’t there • Lets use a for loop to expose this • insert Practical Demo here

  32. Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop

  33. A different for loop • The “for each” loop (aka enhanced for loop) is designed to work with collections like arrays • The syntax is: for(type variableName : collection){ }

  34. Using the for each for(type variableName : collection){ } • primitive or defined by class • eg int or Elephant • Collection you are searching through

  35. In English please...? • Lets say you had an array called myCatArray of Cat objects. • And you wanted to say: • How would that look as a for each loop? For each object of type Cat in the collection called myCatArray do this with the object

  36. for each for each object object of type of type Cat Cat in the collection called in the collection called myCatArray myCatArray do this with the object do this with the object ( : ) { doThisWithThis(object); }

  37. How does it work?

  38. theCurrentCat And so on… for(Cat theCurrentCat: myCatArray){ theCurrentCat.stroke(); } Cat Memory (((PURR))) (((PURR))) (((PURR))) (((PURR))) (((PURR))) (((PURR))) (((PURR))) (((PURR))) (((PURR))) [ ] 2 3 4 5 7 1 8 0 6

More Related