08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming...

35
08 Deterministic iteration 1 23 Mar 2022 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques

Transcript of 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming...

Page 1: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 118 Apr 2023

08 Deterministic iteration

CE00858-1: Fundamental Programming Techniques

Page 2: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 218 Apr 2023

Objectives

In this session, we will:• introduce repetition using a for loop • analyse problems involving repetition• consider loops within loops • implement solutions to problems involving both

repetition and selection

Page 3: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 318 Apr 2023

Control structures

• any program only uses:• sequence• selection • repetition

• so far only written programs using simple sequences• each statement performed in order

• and selections• different statements are performed dependent on

some condition

Page 4: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 418 Apr 2023

Iteration

• doing the same actions repeatedly

while there is more shopping in the basket, put an item on the conveyor belt

loop condition

action

• two types of iteration: • deterministic – know before hand how many times to

repeat• non-deterministic – don’t know how many times to

repeat

Page 5: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

Analysis of iteration

• when faced with a new specification we need to decide whether actions are performed more than once• if they are, there will normally be an iteration in the program

• if a program has an iteration in it, need to consider:• what data is used• what operations are done once before the loop?• how many times is loop repeated?• what operations are repeated inside the loop?• what operations are done once after the loop?

• to decide on how many times the loop is executed, need to consider• when the loop starts• when the loop finishes

05 Simple selection 518 Apr 2023

Page 6: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 618 Apr 2023

Deterministic iteration

• repeating a known number of times

dealing a hand of 7 cards to two players: repeat 7 times

deal card to player 1deal card to player 2

singing 10 green bottlesrepeat 10 times from 10 down to 1

sing 1 verse

• also used to count number of repetitions

Page 7: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 718 Apr 2023

For statement in Java

for (initial statement; condition; increment)

{

statement;}

done once before loop starts

tested before starting each repetition of loop: if true, enter loop if false, exit loop

done just before end of each repetition

braces are needed to block more than one statement

Page 8: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 818 Apr 2023

For loop example – Sum

• problem:• input 10 integers• calculate the sum and output it

Page 9: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 918 Apr 2023

Sum analysis

• what data is used?• num: integer input by user• sum: integer total calculated in program

• what operations are performed?• iteration needed to add up several numbers

• what operations are done once before the loop?• create Scanner• prompt user for number• initialise sum to 0

• how many times is loop repeated?• loop repeated 10 times

• what operations are repeated inside the loop?• input num• add num to sum

• what operations are done once after the loop?• output sum

Page 10: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1018 Apr 2023

Sum code

//calculate and output sum of 10 integersScanner myKeyboard = new Scanner(System.in);System.out.print("Enter 10 integers: ");int sum = 0;int num;

//sum numbers entered by userfor (int i = 0; i < 10; i++){

num = myKeyboard.nextInt();sum = sum + num;

}

System.out.println("Sum is: " + sum);

loop repeated 10 times

statements repeated inside loop

Sum.java

could also put:for (int i = 1; i <= 10; i++)

Page 11: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1118 Apr 2023

Sum loop explanation

• initially i is set to 0• as i is less than 10, the loop is entered• the statements within the loop are executed

• the next number is entered• the number is added to sum

• the value of i is incremented to 1 (i++) • i is now 1 which is still less than 0, the loop is

entered• this continues until i is not less than 10 when

the test will be false• the loop terminates

Page 12: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1218 Apr 2023

Using counter example – Squares

• the variable declared in the for statement can be used within the loop

• problem:• to output the squares of numbers from 1 to 10

Page 13: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1318 Apr 2023

Squares analysis

• what data is used?• i: integer counter to control number of repetitions, used

inside loop• what operations are performed?

• iteration needed to calculate square of several numbers• what operations are done once before the loop?

• none• how many times is loop repeated?

• loop repeated 10 times, i = 1 to 10• what operations are repeated inside the loop?

• square = i * i• output square

• what operations are done once after the loop?• none

Page 14: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1418 Apr 2023

Squares code

//calculate and output squaresfor (int i = 1; i <= 10; ++i ){

System.out.println ( i + " squared is " + (i * i));}

Squares.java

Page 15: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1518 Apr 2023

Count back example – CountDown

• the loop counter can start and end at any value

• problem:• count down from a number entered by the user• when count down finished, "Lift off" should be output

Page 16: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1618 Apr 2023

CountDown analysis

• what data is used?• start: positive integer input by user• count: integer counter to control number of repetitions,

used inside loop• what operations are performed?

• iteration needed to count down from starting value• what operations are done once before the loop?

• create Scanner• prompt user for start• input start

• how many times is loop repeated?• loop repeated start times, count = start down to 0

• what operations are repeated inside the loop?• output count

• what operations are done after the loop?• output "Lift off"

Page 17: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1718 Apr 2023

CountDown code

//count down for lift offScanner myKeyboard = new Scanner(System.in);System.out.print("Enter start value for count down: ");int start = myKeyboard.nextInt();

//count back from starting pointfor (int count = start; count >= 0; count --){

System.out.println(count);}

//lift offSystem.out.println("Lift off!");

CountDown.java

Page 18: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1818 Apr 2023

Common mistake 1

• problem...

for (i = 0; i <= 10; i++)System.out.print("i is: ");System.out.println(i);

Page 19: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 1918 Apr 2023

Common mistake 2

• problem...

for (i = 0; i <= 10; i++);{

System.out.print("i is: ");System.out.println(i);

}

Page 20: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2018 Apr 2023

Common mistake 3

• problem...

for (i = 0; i >= 10; i++){

System.out.print("i is: ");System.out.println(i);

}

Page 21: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2118 Apr 2023

Common mistake 4

• problem...

for (i = 0; i >= 10; i--){

System.out.print("i is: ");System.out.println(i);

}

Page 22: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2218 Apr 2023

Nested loops example – BlockOfFlats

• iteration within an iteration• inner loop completed each time outer loop

executes• problem:

• output address of each flat in a block:• four floors, each containing 3 flats• output as a table

Page 23: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2318 Apr 2023

BlockOfFlats analysis

• what data is used?• floor: integer counter to count 4 floors, used inside loop• flat: integer counter to count 3 flats, used inside loop

• what operations are performed?• iteration needed as there are 4 floors• nested iteration needed as there are 3 flats on each floor

• what operations are done once before loop to process floors?• none

• how many times is loop to process floors repeated?• loop repeated 4 times, floor = 4 down to 1 as we deal with top

floor first• what operations are repeated inside loop to process floors?

• process flats on floor – needs to be expanded• output newline

• what operations are done once after loop to process floors?• none

Page 24: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2418 Apr 2023

• process flats on floor analysis:• how many times is loop to process flats repeated?

• loop repeated 3 times, flat = 1 to 3• what operations are repeated inside loop to process

flats?• output floor and flat number

Page 25: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2518 Apr 2023

BlockOfFlats

//process floorsfor (int floor = 4; floor >= 1; floor--){

//process flats on floorfor (int flat = 1; flat <= 3; flat++){

System.out.println("Floor: " + floor + ", flat: " + flat);}System.out.println();

}

BlockOfFlats.java

Page 26: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2618 Apr 2023

Sequence of loops example – Triangle

• problem:• output triangle of stars:

**** *** ** *

Page 27: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2718 Apr 2023

Triangle analysis

Row Pattern Spaces Stars

1 **** 0 4

2 -*** 1 3

3 --** 2 2

4 ---* 3 1

General case:

row – 1 5 - row

Page 28: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2818 Apr 2023

Triangle analysis continued

• what data is used?• row: integer loop counter to count 4 rows, used inside loop• space: integer loop counter to count number of spaces on row, used

inside loop • star: integer loop counter to count number of stars on row, used inside

loop• what operations are performed?

• iteration needed as there are 4 rows• nested iteration as there are several spaces on each row• followed by second nested iteration as there are several stars on each row

• what operations are done once before row loop?• none

• how many times is row loop repeated?• loop repeated 4 times, row = 1 to 4

• what operations are repeated inside row loop?• process spaces on row – needs to be expanded• process stars on row – needs to be expanded• output newline

• what operations are done once after row loop?• none

Page 29: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 2918 Apr 2023

• process spaces on row analysis:• how many times is spaces loop repeated?

• loop repeated row – 1 times, space = 1 to row - 1• what operations are done inside spaces loop?

• output single space

• process stars on row analysis:• how many times is stars loop repeated?

• loop repeated 5 – row times, star = 1 to 5 - row • what operations are done inside the stars loop?

• output single star

Page 30: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 3018 Apr 2023

Triangle code

//process rowsfor (int row = 1; row <= 4; row++){

//process spaces on rowfor (int space = 1; space <= row - 1; space++){

System.out.print(" ");}//process stars on rowfor (int star = 1; star <= 5 - row; star++){

System.out.print("*");}System.out.println();

}

Triangle.java

Page 31: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 3118 Apr 2023

Selection in iteration example - Smallest

• can also perform tests within iteration

• problem:• read in 10 integers• output smallest number read in

Page 32: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 3218 Apr 2023

Smallest analysis

• what data is used?• num: integer input by user• smallest: integer to hold smallest value, determined in program

• what operations are performed?• iteration needed to find smallest of 10 integers input• nested selection needed as smallest number needs to be stored

• what operations are done once before loop?• create Scanner• prompt user for input• read first number and store in smallest

• how many times is loop repeated?• 9 times as first integer read outside loop, i = 1 to 9

• what operations are repeated inside the loop?• read number• check if smallest number – needs to be expanded

• what operations are done once after the loop?• output smallest

Page 33: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 3318 Apr 2023

• check if smallest number:• what operations are done if num less than smallest?

• store num in smallest• what operations are done if num not less than

smallest?• none

Page 34: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 3418 Apr 2023

Smallest code

//output smallest number in a list of 10Scanner myKeyboard = new Scanner(System.in);System.out.println("Enter 10 integers: ");int smallest = myKeyboard.nextInt();

for (int i = 1; i < 10; i++){

int num = myKeyboard.nextInt();if (num < smallest){

smallest = num;}

}

//output smallestSystem.out.println("Smallest number: " + smallest);

Smallest.java

Page 35: 08 Deterministic iteration1May 15 08 Deterministic iteration CE00858-1: Fundamental Programming Techniques.

08 Deterministic iteration 3518 Apr 2023

Summary

In this session we have:• covered the for loop and how it is used when the

number of repetitions is known in advance• analysed problems involving deterministic iteration• implemented the for loop• seen how loops can contain nested loops and other

control structures

In the next session we will:• consider non-deterministic iteration