www.hope.ac.uk faculty of sciences and social sciences hope java: loops within loops stewart...
Post on 15-Jan-2016
214 views
TRANSCRIPT
![Page 1: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/1.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java: Loops within loops
Stewart Blakeway
FML 213
![Page 2: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/2.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 3: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/3.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 4: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/4.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we shall do today
• Nested for loops
4
![Page 5: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/5.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (i = 1; i <= 3; i++)
{
}
5
Loop Recap
ONLY when NOT already executing loop
1
![Page 6: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/6.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (i = 1; i <= 3; i++)
{
}
6
2
![Page 7: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/7.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (i = 1; i <= 3; i++)
{
}
7
3
execute instruction execute instruction . . . execute instruction
TRUE
![Page 8: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/8.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PEfor (i = 1; i <= 3; i++)
{
}
8
4
![Page 9: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/9.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PEfor (i = 1; i <= 3; i++)
{
}
9
5
![Page 10: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/10.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Repetition
• Keep doing (iterating, repeating, looping)
• Until 2 evaluates to FALSE
2,3,4 and 5
![Page 11: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/11.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PEfor (i = 1; i <= 3; i++)
{
}
11
5+2
execute instruction . . . execute instruction
FALSE
![Page 12: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/12.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (i = 1; i <= 3; i++)
{
}
Will do this 3 times
12
Loop Recap
![Page 13: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/13.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (j = 1; j <= 5; j++)
{
}
Will do this 5 times
13
![Page 14: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/14.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (i = 1; i <= 3; i++)
{
}
Will do this 3 times
14
for (j = 1; j <= 5; j++)
{
}
Will do this 5 times
![Page 15: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/15.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
i = 1
i = 2
i = 3
j = 1, 2, 3, 4, 5
j = 1, 2, 3, 4, 5
j = 1, 2, 3, 4, 5
15
![Page 16: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/16.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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?
![Page 17: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/17.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
A variation of a loop within a loop
i = 1 j goes from 1 TO 1
17
i = 2
i = 3
j goes from 1 TO 2
j goes from 1 TO 3
![Page 18: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/18.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
18
Pencil & Paper exercises in pairsPredict the resulting output for each of the following programs.
![Page 19: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/19.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
int i;
int j;
for ( i = 1; i <= 5; i++)
{
for ( j = 1; j <= 8; j++)
{
System.out.print("X");
}
System.out.println();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
19
Exercise 1
![Page 20: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/20.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 21: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/21.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 22: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/22.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 23: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/23.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 24: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/24.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
XX
XXX
X
XX
XXX
XXXX
24
Exercise 4
![Page 25: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/25.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
XX
XXX
X
XX
XXX
XXXX
25
Exercise 4
![Page 26: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/26.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
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
![Page 27: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/27.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Careful with Loops!
for (i = 1; i <= 3; i++)
{
}
for (i = 1; i <= 5; i++)
{
}
![Page 28: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/28.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Careful with Loops!
for (i = 1; i <= 5; i++)
{
}
for (i = 1; i <= 3; i++)
{
}
![Page 29: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/29.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Careful with Loops!
for (i = 1; i <= 3; i++)
{
}
for (i = 1; i <= j; j++)
{
}
![Page 30: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader036.vdocuments.net/reader036/viewer/2022070412/56649d2f5503460f94a06d8a/html5/thumbnails/30.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any Questions?
• Next Week– Strings