Comp1004: Loops and Arrays I

# Comp1004: Loops and Arrays I

## Comp1004: Loops and Arrays I

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Comp1004: Loops and Arrays I Whiles, For and Arrays[]

2. HouseKeeping • Coursework 1 has been set • Worth 10% of your module marks • “The aim of this coursework is to model a pack of playing cards, write a method to shuffle the pack of cards and write a short game of ‘higher or lower’ ” • Deadline: Friday 18th November 2011 at 17.00 • via https://handin.ecs.soton.ac.uk • Also – Lab Marking!

3. Overview • Looping • while • do while • for • Arrays • indexes • For each loop

4. LoopingWhile: The King of Loops

5. 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 • Aprogramming structure that performs a series of instructions repeatedly until some specified condition is met

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

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

8. While while(condition){ code to run; } Just like an if statement condition if(condition) { code to run; } Zero or More N.B. The condition is checked at the start of the loop – so the code inside may not run at all

9. While – Example 1 inti = 0; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement?

10. While – Example 1 inti = 0; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement? i starts as 0 and the loop stops when i becomes equal to 10. So the loop will print the numbers 0-9 and the final statement will print number 10

11. While – Example 3 inti = 10; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement?

12. While – Example 3 inti = 10; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement? i starts as 10 and the loop stops when i becomes equal to 10. So the loop will not start at all and the final statement will print number 10

13. While – Example 4 inti = 0; while(i<10){ System.out.println(“The number is ” + i); i= i*1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement?

14. While – Example 4 inti = 0; while(i<10){ System.out.println(“The number is ” + i); i= i*1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement? This is called an infinite loop. They cause programs to hang and/or crash. Always check that you loops will end!

15. LoopingOther Loops

16. Do while int count = 1; do { System.out.println("Count is: " + count); count++; }while (count <= 11); One or More N.B. The condition is checked at the end of the loop – so the code inside will always run at least once

17. For loop for (initialization; condition; statechange) { statement(s) } Counting The for loop is a simple way of repeating a block of code when you know in advance how many iterations (times around the loop) that you want

18. For loop initialization; condition; statechange for (inti = 0; i < 10; i = i+ 1) { System.out.println(i); } Counting The for loop is a simple way of repeating a block of code when you know in advance how many iterations (times around the loop) that you want

19. For loop initialization; condition; statechange for (inti = 0; i < 10; i = i+ 1) { System.out.println(i); } Counting The for loop is a simple way of repeating a block of code when you know in advance how many iterations (times around the loop) that you want inti = 0; while(i < 10){ System.out.println(i); i = i + 1; } You can use a while loop to achieve the same thing. But the for loop is neater

20. Arrays

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

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

23. Details of Arrays • To instantiate an Array: numberStore = new int[10]; This is the length you want the array to be (the number of cups) N.B that we are creating an object here (using the new keyword), but we don’t call a constructor. Arrays have their own special syntax.

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

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

26. Using indexes (insertion)

27. Using indexes (insertion) numStore int[] numStore; int[]

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

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

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

31. Using indexes (insertion) numStore int[] numStore; numStore = new int[9]; numStore[0] = 77; System.out.println(numStore[0]); [ ] 77 2 3 4 5 7 1 8 0 6 int[] Declaration Instantiation Assignment Retrieval

32. 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)

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

34. Iterating Over an Array intnumStore = new int[9]; //some missing code to fill the array with values for(inti = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Arrays are powerful because we can use a variable as the array index. This means we can iterate over them in a loop

35. Iterating Over an Array intnumStore = new int[9]; //some missing code to fill the array with values for(inti = 0; i < 10; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } But make sure the index is always valid (in bounds). Otherwise the program will crash when it reaches the line that tries to use an index that is out of bounds

36. Iterating Over an Array 2 Iterating over an array is so common that Java now includes a loop specifically to do it. intnumStore = new int[9]; //some missing code to fill the array with values for(inti = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); }

37. Iterating Over an Array 2 Iterating over an array is so common that Java now includes a loop specifically to do it. intnumStore = new int[9]; //some missing code to fill the array with values for(inti = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } • The “for each” loop (aka enhanced for loop) is designed to work with collections like arrays • The syntax is: for(type variableName : collection){ }

38. Iterating Over an Array 2 Iterating over an array is so common that Java now includes a loop specifically to do it. intnumStore = new int[9]; //some missing code to fill the array with values for(inti = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Like the for loop the ‘for each’ loop is a shortcut, that is a bit neater than writing the code the long way. intnumStore = new int[9]; //some missing code to fill the array with values for(int n : numStore){ System.out.println(“Number is ” + n); }

39. Iterating Over an Array 2 Iterating over an array is so common that Java now includes a loop specifically to do it. intnumStore = new int[9]; //some missing code to fill the array with values for(inti = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Like the for loop the ‘for each’ loop is a shortcut, that is a bit neater than writing the code the long way. intnumStore = new int[9]; //some missing code to fill the array with values for(int n : numStore){ System.out.println(“Number is ” + n); } But it can only be used for access (e.g. n++ would not increment the value in the array) And it hides the current index

40. Summary • Looping • while • do while • for • Arrays • indexes • For each loop