demonstrate use of a “for loop” in the design and development of a java program to calculate the...

16
Lecture 9.2 Revision of arrays and “for” loops

Upload: lenard-day

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

Lecture 9.2

Revision of arrays and “for” loops

Page 2: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.

Design your test data (use numbers that will be easy to check for the correct answer):◦ 10, 20, 30, 40, 50, 60◦ Expected total is 210.

Design and code a solution to the problem.

Coding from a design

Page 3: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

while (condition) { // if false then loop not executedstatement(s) inside the loop

} // end while

do { // always done at least oncestatement(s) inside the loop

} while (condition) // end do

for (control initial value; (condition) ; control update) {// if false then loop not executed statement(s) inside the loop} // end for

Different types of loop

Condition is always either true or false.

while and do: something must be updated inside the loop which affects the condition (so that it is not an endless loop).

for: something is updated inside the “for” statement which affects the condition (so that it is not an endless loop).

e.g. while (i < 4) { System.out.println("i "= " + i); i = i + 1; } // end while

Page 4: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

for (it is raining; while it rains; check if it is raining) {I will need my umbrella} // end for(the for loop has ended therefore the condition must now be false)

// I do not need my umbrella because it is not raining.

The for loop will not be executed if the condition is false, just like the while loop.

For loop example conditionthen clause

Remember the while loop example?while (it is raining) {

then I will need my umbrella} // end while// Now I do not need my umbrella// (because it must have stopped raining)

For loop (poor) example

Page 5: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

for (it is raining; while it rains; check if it is raining) {I will need my umbrella

} // end for// (now the condition must be false)// Now I do not need my umbrella because it must not be raining.

For loop formatconditionthen clause

initialisation statement

loop condition

increment statement

Normally we use a for loop when we know how many times we want to execute the loop.

Page 6: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

for (i = 0; i < 5; i++) {statement(s)

} // end for

Has the same effect as:

For loop example conditionthen clause

loop control variable is i

initialisation statement i = zero

loop condition while i < 5 is true

int i = 0;while (i<5){ statement(s) i++; // must not forget to update the control variable} // end while

increment statement add 1 to i

Page 7: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

if (it is raining) {then I will need my umbrella

for (it is raining; while it is raining; check for rain) { then I will need my umbrella

} // end for} // end ifNow I do not need my umbrella

if (your_age < 65) for (the_age = your age; the_age < 65; the_age++) {

then you cannot get your state pension } // end for} // end ifNow you can get your state pension

for and if example conditionthen clause

initialisation statement

loop condition

increment statement

Page 8: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

for (it is raining or snowing; it is raining or snowing; check weather)then I will need my umbrella

} // end while// I will not need my umbrella //(because it is not raining and it is not snowing)

for (it is sunny and hot; it is sunny and hot; check weather) {then I will need a sunshade and a cold drink

} // end while// I will not need a sunshade and a cold drink// (this means it is not both sunny and hot// it might be sunny but cold, or it might be cloudy but hot, // either way I have no sunshade and get no drink)

Complex expression (and/or)

initialisation statement

loop conditionincrement statement

Page 9: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

import javax.swing.JOptionPane;public class week9 { public static void main (String[] args) { int i=0; int j=0; for (i = 1; i < 5 && j < 5; i++) {

// statements }

System.out.println("i = " + i + " j = " + j); }}

Complex expression (and)

initialisation statement

loop condition

increment statement

Page 10: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

import javax.swing.JOptionPane;public class week9 { public static void main (String[] args) { int i=0; int j=0; for (i = 1; i < 5 || j < 5; i++) {

// statements }

System.out.println("i = " + i + " j = " + j); }}

What would happen if we ran this program?

Complex expression (or)

initialisation statement

loop condition

increment statement

Page 11: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

for (i = 1; i < 5; i = i + 1) { statement(s) for (j = 1; j < 5; j = j + 1) {

statement(s)

} // end for

} // end for

Nested for initialisation statement

initialisation statement

loop condition

loop condition

increment statement

increment statement

Page 12: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.

Design your test data (use numbers that will be easy to check for the correct answer):◦ 10, 20, 30, 40, 50, 60◦ Expected total is 210.

Design and code a solution to the problem.

Demonstrate use of “for loop”

Think about the meaning.

How can we use a for loop here?

Normally we use a for loop when we know how many times we want to execute the loop.

We could use a loop to access each element of the array one at a time.

We will need a variable to hold the total, and a variable to count the elements of the array.

Page 13: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

1. Get unknown data and create variables.1.1 create an array with values 10, 20, 30, 40, 50, 60.1.2 create and zeroise total.1.3 create and zeroise counter.

2. Processing.2.1 count for six times 2.2 add current array value to total2.3 end for

3. Output.3.1 Output the total.

Algorithm with for

Page 14: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

Activity Diagram

22

[count >= 6]

Zeroise total

Total = total + array[count]

[count < 6]

Output total

Array = 10, 20, 30, 40, 50, 60

Zeroise counter

Add 1 to count

Page 15: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

//1. Get unknown data and create variables.//1.1 create an array with values 10, 20, 30, 40, 50, 60. //1.2 create and zeroise total.

// 1.3 create and zeroise counter.

//2. Processing.//2.1 count for six times

//2.2 add current array value to total

//2.3 end for

//3. Output.

//3.1 Output the total.

Make algorithm into comments

Page 16: Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design

//1. Get unknown data and create variables.//1.1 create an array with values 10, 20, 30, 40, 50, 60.int [] number = {10, 20, 30, 40, 50, 60}; //1.2 create and zeroise total.int total = 0;// 1.3 create and zeroise counter.int count = 0;

//2. Processing.//2.1 for six times

for (count=0;count<6;count++) {//2.2 add current array value to total

total = total + number[count];//2.3 end for

} // end for//3. Output.

//3.1 Output the total.System.out.println("The total is " + total);

Add the code

Remember in Java the first element of an array is referred to as position zero.