Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Seen what an algorithm is – a set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms in – Structured English – Flow charts Used variables to remember 2

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Applied the top down, stepwise refinement approach to creating algorithms Looked at problems more oriented towards being solved on a computer – stacks, queues, functions, procedures Seen how high level languages like Java are used to create programs with the aide of compilers The problem solving constructs in Java 3

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today Nested for loops 4

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 5 Loop Recap ONLY when NOT already executing loop 1

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 6 2

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 7 3 execute instruction. execute instruction TRUE

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 8 4

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 9 5

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Repetition Keep doing (iterating, repeating, looping) Until 2 evaluates to FALSE 2,3,4 and 5

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } 11 5+25+2 execute instruction. execute instruction FALSE

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } Will do this 3 times 12 Loop Recap

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (j = 1; j <= 5; j++) { } Will do this 5 times 13

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for (i = 1; i <= 3; i++) { } Will do this 3 times 14 for (j = 1; j <= 5; j++) { } Will do this 5 times

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE i = 1 i = 2 i = 3 j = 1, 2, 3, 4, 5 15

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A variation of a loop within a loop 16 for (i = 1; i <= 3; i++) { } Will do this 3 times for (j = 1; j <= i; j++) { } How many times?

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE A variation of a loop within a loop i = 1j goes from 1 TO 1 17 i = 2 i = 3 j goes from 1 TO 2 j goes from 1 TO 3

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 18 Pencil & Paper exercises in pairs Predict the resulting output for each of the following programs.

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 1; i <= 5; i++) { for ( j = 1; j <= 8; j++) { System.out.print("X"); } System.out.println(); } XXXXXXXX 19 Exercise 1

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 1; i <= 5; i++) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } 20 Exercise 2

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 1; i <= 5; i++) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } X XX XXX XXXX XXXXX 21 Exercise 2

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 5; i >= 1; i--) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } 22 Exercise 3

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; for ( i = 5; i >= 1; i--) { for ( j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } XXXXX XXXX XXX XX X 23 Exercise 3

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; int k; for ( i = 1; i <= 4; i++) { for ( j = 1; j <= i ; j++) { for ( k = 1; k<= j; k++) { System.out.print("X"); } System.out.println(); } System.out.println(); } X X XX X XXX X XX XXX XXXX 24 Exercise 4

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; int k; for ( i = 1; i <= 4; i++) { for ( j = 1; j <= i ; j++) { for ( k = 1; k<= j; k++) { System.out.print("X"); } System.out.println(); } System.out.println(); } X X XX X XXX X XX XXX XXXX 25 Exercise 4

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int i; int j; int k; int count; for ( i = 1; i <= 5; i++) { count = 0; for ( j = 1; j <= i; j++) { for ( k = 1; k<= j; k++) { count++; } System.out.println(count); } 1 3 6 10 15 26 Exercise 5

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Careful with Loops! for (i = 1; i <= 3; i++) { } for (i = 1; i <= 5; i++) { }

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Careful with Loops! for (i = 1; i <= 5; i++) { } for (i = 1; i <= 3; i++) { }

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Careful with Loops! for (i = 1; i <= 3; i++) { } for (i = 1; i <= j; j++) { }

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Any Questions? Next Week – Strings


Download ppt "Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213"

Similar presentations


Ads by Google