Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart...
-
date post
15-Jan-2016 -
Category
Documents
-
view
213 -
download
0
Transcript of Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart...
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java: Loops within loops
Stewart Blakeway
FML 213
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
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we shall do today
• Nested for loops
4
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (i = 1; i <= 3; i++)
{
}
6
2
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PEfor (i = 1; i <= 3; i++)
{
}
8
4
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PEfor (i = 1; i <= 3; i++)
{
}
9
5
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
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
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
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
for (j = 1; j <= 5; j++)
{
}
Will do this 5 times
13
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
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
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?
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
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.
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
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
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
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
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
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
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
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
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++)
{
}
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++)
{
}
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++)
{
}
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any Questions?
• Next Week– Strings