380 likes | 725 Views
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
E N D
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 • a programming structure that performs a series of instructions repeatedly until some specified condition is met
There are different kinds of loops... • while • do while • for • for each (enhanced for) • Recursion (not really a loop)
Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop
While • The fundamental loop • All of the other loops can be built from this
Look at While while(condition){ code to run; } Just like an if statement condition
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?
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);
Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop
Do while int count = 1; do { System.out.println("Count is: " + count); count++; } while (count <= 11); Always runs at least once
Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop
For loop for (initialization; termination; increment) { statement(s) }
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
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
Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop
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
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
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
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.
Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop
Index An index is the position in the array. Java arrays start at 0 [ ] 2 3 4 5 7 1 8 0 6
Length The length is the number of indexes Here the length is 9 [ ] 2 3 4 5 7 1 8 0 6
Using indexes (insertion) int[] numStore; numStore = new int[9]; numStore[0] = 77; [ ] 2 3 4 5 7 1 8 0 6
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 []
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
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
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.
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)
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
Lecture contents • Looping • while • do while • for • Arrays • indexes • For each loop
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){ }
Using the for each for(type variableName : collection){ } • primitive or defined by class • eg int or Elephant • Collection you are searching through
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
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); }
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