+ All Categories
Home > Documents > Comp1004: Loops and Arrays I

Comp1004: Loops and Arrays I

Date post: 22-Feb-2016
Category:
Upload: abena
View: 38 times
Download: 0 times
Share this document with a friend
Description:
Comp1004: Loops and Arrays I. Whiles, For and Arrays[]. 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’ ” - PowerPoint PPT Presentation
Popular Tags:
40
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
Transcript
Page 1: Comp1004: Loops and Arrays I

Comp1004: Loops and Arrays IWhiles, For and Arrays[]

Page 2: Comp1004: Loops and Arrays I

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!

Page 3: Comp1004: Loops and Arrays I

Overview

• Looping– while– do while– for

• Arrays– indexes

• For each loop

Page 4: Comp1004: Loops and Arrays I

LoopingWhile: The King of Loops

Page 5: Comp1004: Loops and Arrays I

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

Page 6: Comp1004: Loops and Arrays I

There are different kinds of loops...

• while• do while• for• for each (enhanced for)• Recursion (not really a loop)

Page 7: Comp1004: Loops and Arrays I

While

• The fundamental loop

• All of the other loops can be built from this

Page 8: Comp1004: Loops and Arrays I

While

while(condition){code to run;

}

Just like an if statement condition

if(condition) { code to run;}Zero or MoreN.B. The condition is checked at the start of the loop – so the code inside may not run at all

Page 9: Comp1004: Loops and Arrays I

While – Example 1int i = 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?

Page 10: Comp1004: Loops and Arrays I

While – Example 1int i = 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

Page 11: Comp1004: Loops and Arrays I

While – Example 3int i = 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?

Page 12: Comp1004: Loops and Arrays I

While – Example 3int i = 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

Page 13: Comp1004: Loops and Arrays I

While – Example 4int i = 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?

Page 14: Comp1004: Loops and Arrays I

While – Example 4int i = 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!

Page 15: Comp1004: Loops and Arrays I

LoopingOther Loops

Page 16: Comp1004: Loops and Arrays I

Do whileint count = 1;

do { System.out.println("Count is: " +

count); count++;

} while (count <= 11); One or MoreN.B. The condition is checked at the end of the loop – so the code inside will always run at least once

Page 17: Comp1004: Loops and Arrays I

For loopfor (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

Page 18: Comp1004: Loops and Arrays I

For loopfor (int i = 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

initialization; condition; statechange

Page 19: Comp1004: Loops and Arrays I

For loopfor (int i = 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

int i = 0;while(i < 10){

System.out.println(i);i = i + 1;

}

initialization; condition; statechange

You can use a while loop to achieve the same thing.

But the for loop is neater

Page 20: Comp1004: Loops and Arrays I

Arrays

Page 21: Comp1004: Loops and Arrays I

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[ ]10 2 3 4 5 6 7 8

Page 22: Comp1004: Loops and Arrays I

Details of Arrays

• Arrays are objects• But they have their own special syntax

• To declare an Array:

int[] numberStore;

or

int numberStore[];

Both of these are valid.

They do the same thing!

Page 23: Comp1004: Loops and Arrays I

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.

Page 24: Comp1004: Loops and Arrays I

Index

An index is the position in the array.Java arrays start at 0

[ ]10 2 3 4 5 6 7 8

Page 25: Comp1004: Loops and Arrays I

Length

The length is the number of indexesHere the length is 9

[ ]10 2 3 4 5 6 7 8

Page 26: Comp1004: Loops and Arrays I

Using indexes (insertion)

Page 27: Comp1004: Loops and Arrays I

Using indexes (insertion)

int[] numStore;

numStore

int[]

Page 28: Comp1004: Loops and Arrays I

Using indexes (insertion)

int[] numStore;numStore = new int[9];

[ ]10 2 3 4 5 6 7 8

numStore

int[]

Page 29: Comp1004: Loops and Arrays I

Using indexes (insertion)

int[] numStore;numStore = new int[9];numStore[0] = 77;

[ ]10 2 3 4 5 6 7 8

77

numStore

int[]

Page 30: Comp1004: Loops and Arrays I

Using indexes (insertion)

int[] numStore;numStore = new int[9];numStore[0] = 77;

[ ]10 2 3 4 5 6 7 8

77

numStore

int[]

Declaration

Instantiation

Assignment

Page 31: Comp1004: Loops and Arrays I

Using indexes (insertion)

int[] numStore;numStore = new int[9];numStore[0] = 77;System.out.println(numStore[0]);

[ ]10 2 3 4 5 6 7 8

77

numStore

int[]

Declaration

Instantiation

Assignment

Retrieval

Page 32: Comp1004: Loops and Arrays I

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)

Page 33: Comp1004: Loops and Arrays I

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

Page 34: Comp1004: Loops and Arrays I

Iterating Over an Arrayint numStore = new int[9];//some missing code to fill the array with values

for(int i = 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

Page 35: Comp1004: Loops and Arrays I

Iterating Over an Arrayint numStore = new int[9];//some missing code to fill the array with values

for(int i = 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

Page 36: Comp1004: Loops and Arrays I

Iterating Over an Array 2int numStore = new int[9];//some missing code to fill the array with values

for(int i = 0; i < 9; i++){System.out.print(“Number at position ” + i);System.out.println(“ is ” + numStore[i]);

}

Iterating over an array is so common that Java now includes a loop specifically to do it.

Page 37: Comp1004: Loops and Arrays I

Iterating Over an Array 2int numStore = new int[9];//some missing code to fill the array with values

for(int i = 0; i < 9; i++){System.out.print(“Number at position ” + i);System.out.println(“ is ” + numStore[i]);

}

Iterating over an array is so common that Java now includes a loop specifically to do it.

• The “for each” loop (aka enhanced for loop) is designed to work with collections like arrays

• The syntax is:

for(type variableName : collection){

}

Page 38: Comp1004: Loops and Arrays I

Iterating Over an Array 2int numStore = new int[9];//some missing code to fill the array with values

for(int i = 0; i < 9; i++){System.out.print(“Number at position ” + i);System.out.println(“ is ” + numStore[i]);

}

Iterating over an array is so common that Java now includes a loop specifically to do it.

int numStore = new int[9];//some missing code to fill the array with values

for(int n : numStore){System.out.println(“Number is ” + n);

}

Like the for loop the ‘for each’ loop is a shortcut, that is a bit neater than writing the code the long way.

Page 39: Comp1004: Loops and Arrays I

Iterating Over an Array 2int numStore = new int[9];//some missing code to fill the array with values

for(int i = 0; i < 9; i++){System.out.print(“Number at position ” + i);System.out.println(“ is ” + numStore[i]);

}

Iterating over an array is so common that Java now includes a loop specifically to do it.

int numStore = new int[9];//some missing code to fill the array with values

for(int n : numStore){System.out.println(“Number is ” + n);

}

Like the for loop the ‘for each’ loop is a shortcut, that is a bit neater than writing the code the long way.

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

Page 40: Comp1004: Loops and Arrays I

Summary

• Looping– while– do while– for

• Arrays– indexes

• For each loop


Recommended