www.hope.ac.uk faculty of sciences and social sciences hope variables and trace tables stewart...
Post on 19-Dec-2015
216 views
TRANSCRIPT
![Page 1: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/1.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables and Trace Tables
Stewart Blakeway
FML 213
http://hopelive.hope.ac.uk/computing/
![Page 2: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/2.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we have done so far
• Introduced Java as a programming language with three constructs:– Sequence– Selection– Repetition
• Introduced algorithms expressed in:– Structured English– Flow charts
2
![Page 3: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/3.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we shall do today
• Introduce the concept of a variable• Apply variables to algorithms• Introduce the trace table as a method of
checking the operation of a program by monitoring its variables
3
![Page 4: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/4.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Counting & Trace Tables
• Pages 29 to 36
4
![Page 5: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/5.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any job we do requires short term memory
• Coming to class– Where is it?– What time does it start?
• Getting a round in– Who ordered what?
5
What time is it ?
![Page 6: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/6.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Some things we remember have to be continually updated
• Following a recipe– Where are we up to?
• Counting people coming into a room– What was the last count?
• Watching a game and you can’t see the scoreboard– What score is it?
6
![Page 7: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/7.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question:• How do we record what we need to
remember in algorithms ?
7
![Page 8: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/8.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question:• How do we record what we need to
remember in algorithms ?
8
Answer:Use
variables
![Page 9: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/9.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables
• A variable is somewhere in memory you store a piece of information you want to remember
9
![Page 10: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/10.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Variables
• A variable is somewhere in memory you store a piece of information you want to remember
• As you work through a task, that piece of information stored in memory may get
updated – its contents vary over time
10
![Page 11: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/11.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Representing a variable
11
14Number of people in the room
2-1Current score
• A box represents an area of memory
• The box is labelled
![Page 12: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/12.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Rules for naming variables
12
14NumberOfPeopleInRoom
2-1CurrentScore
![Page 13: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/13.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Rules for naming variables
• No spaces in the name• Use capitals to make name more readable• Preparing for converting to a computer
programming language where normally no spaces are allowed in variable names
13
14NumberOfPeopleInRoom
2-1CurrentScore
![Page 14: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/14.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in Java
int NumberOfPeopleInRoom;
String CurrentScore;
14
![Page 15: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/15.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in Java
int NumberOfPeopleInRoom;
String CurrentScore;
15
![Page 16: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/16.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in Java
int NumberOfPeopleInRoom;
String CurrentScore;
NumberOfPeopleInRoom = 25;
CurrentScore = "2-1";
16
![Page 17: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/17.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Making variables in algorithms
NumberOfPeopleInRoom := 25
CurrentScore := “2-1”
17
Making the variable and putting in the first value are done in one step
![Page 18: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/18.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Count := 0
This does 2 things
1. Establishes an area of memory called Count
2. Puts zero into this location
18
?????Count
0Count
![Page 19: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/19.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
• This can be read as
– becomes
or –becomes equal to
19
:=
![Page 20: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/20.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Fred := 12
20
???????1 Fred
2 12Fred
There is no significance in the name of the variable
It is not a keyword like while, if, …
It is up to the programmer
![Page 21: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/21.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Total := 0Total := Total + 1
21
These 2 lines do 3 things.
1
2
3
?????Total
0
1
Total
Total
Set up the variable Total
Initialise it to zero
The new contents of Total become equal to the old
contents plus 1
![Page 22: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/22.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
display Sum
22
156Sum
156
Copies the contents of the variable Sum to the default output device
![Page 23: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/23.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
read(Fred)
23
This does three things
1
2
3
Establishes a variable called Fred
???Fred
Waits for input from the input device:
For example key press 8
Places 8 into the variable Fred
8Fred
![Page 24: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/24.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Reading input in Java
int Fred;
System.in.read(Fred);
24
![Page 25: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/25.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
25
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
![Page 26: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/26.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
26
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
?
?
Amanda
Brian?
Alma
Variables
![Page 27: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/27.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
0
?
Amanda
Brian?
Alma
Variables
![Page 28: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/28.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
28
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
0
?
Amanda
Brian0
Alma
Variables
![Page 29: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/29.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
29
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
Assume that 6 is input
0
6
Amanda
Brian0
Alma
Variables
![Page 30: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/30.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
30
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
Alma is greater than 0
so we go into loop body
0
6
Amanda
Brian0
Alma
Variables
![Page 31: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/31.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
31
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
6
Amanda
Brian0
Alma
Variables
![Page 32: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/32.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
32
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
6
Amanda
Brian6
Alma
Variables
![Page 33: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/33.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
33
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
4
Amanda
Brian6
Alma
Variables
Assume that 4 is input
![Page 34: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/34.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
34
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
1
4
Amanda
Brian6
Alma
Variables
Alma is greater than 0
so we go into loop body
![Page 35: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/35.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
35
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
4
Amanda
Brian6
Alma
Variables
![Page 36: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/36.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
36
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
4
Amanda
Brian10
Alma
Variables
![Page 37: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/37.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
37
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
0
Amanda
Brian10
Alma
Variables
Assume that 0 is input
![Page 38: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/38.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
38
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
0
Amanda
Brian10
Alma
Variables
Alma is no longer greater than 0
so the loop is finished
![Page 39: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/39.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Putting it altogether so far
39
Brian := 0Amanda := 0read(Alma)while Alma greater than 0
begin Brian := Brian + 1Amanda := Amanda + Almaread(Alma)
end display Brian , Amanda
2
0
Amanda
Brian10
Alma
Variables
2
10
![Page 40: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/40.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java versionint Brian;int Stewart;int Alma;
Brian = 0;Stewart = 0;System.in.read(Alma);while (Alma > 0) { Brian = Brian + 1; Stewart = Stewart + Alma; System.in.read(Alma); }System.out.println(Brian , Stewart);
40
![Page 41: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/41.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Good Practice: Variable Names
41
Variable names should reflect their intended purpose.
This purpose, like a well designed user interface, should be guessable.
This helps with the readability of a program, together with comments.
What is the intended purpose of each of the variables: Brian, Stewart and Alma ?
![Page 42: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/42.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Better Variable Names
42
1 is added to the contents of Brian (initially zero) each time through the while loop.
Each number input is stored in (not added to) the variable Alma.
The value stored in Alma is added to the current value of Stewart (initially zero).
![Page 43: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/43.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
The Names at Last
43
Brian seems to be counting each loop.
Alma seems to be storing each number input
Stewart seems to be keeping a running total.
Brian Count
Alma Number
Stewart Total
Refer to your course booklet on page 31
![Page 44: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/44.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Trace Tables
44
The above method of keeping track of the contents of variables is obviously tedious. A better way would be to employ a tabular method. This is called a trace table.
Consider the following algorithm :
A := 6 (Step 1)
B := 8 (Step 2)
C := A (Step 3)
A := B (Step 4)
B := C (Step 5)
![Page 45: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/45.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
The Empty Trace Table
45
Step Number
A B C
123
4
5
Variables
A := 6B := 8C := AA := BB := C
![Page 46: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/46.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Here we go
46
Step Number
A B C
1 623
4
5
A := 6B := 8C := AA := BB := C
![Page 47: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/47.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
47
Step Number
A B C
1 62 83
4
5
A := 6B := 8C := AA := BB := C
![Page 48: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/48.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
48
Step Number
A B C
1 62 83 6
4
5
A := 6B := 8C := AA := BB := C
![Page 49: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/49.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
49
A := 6B := 8C := AA := BB := C
Step Number
A B C
1 6
2 8
3 6
4 8
5
![Page 50: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/50.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next Step
50
A := 6B := 8C := AA := BB := C
Step Number
A B C
1 6
2 8
3 6
4 8
5 6
![Page 51: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/51.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Tracing a Loop
51
Answer := 1(Step 1)I := 0 (Step 2)Number := 0(Step 3)while (I is less than 3 ) (Step 4) begin
read(Number) (Step 5)Answer := Answer * Number (Step 6)I := I + 1 (Step 7) end display Answer
Multiply
Steps are numbered wherever variable contents are changed or conditions are tested.
![Page 52: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/52.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Here is the Blank Trace Table Step
NumberAnswe
rI Numbe
rIs I < 3 ?
52
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 53: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/53.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation stepsStep Number
Answer
I Number
Is I < 3 ?
1 1
53
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 54: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/54.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation stepsStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
54
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 55: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/55.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initialisation stepsStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
55
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 56: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/56.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Now the Test: Is 0 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
56
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 57: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/57.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Into the LoopStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
57
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Assume 5 is input
![Page 58: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/58.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Into the LoopStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
58
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
5 times 1 = 5
![Page 59: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/59.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Into the LoopStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
59
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
1 is added to I
![Page 60: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/60.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 1 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
60
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 61: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/61.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
61
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Assume 2 is input
![Page 62: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/62.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
62
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
5 times 2 = 10
![Page 63: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/63.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
63
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
1 is added to I
![Page 64: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/64.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 2 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
64
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 65: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/65.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
65
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Assume 4 is input
![Page 66: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/66.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
66
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
10 times 4 = 40
![Page 67: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/67.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Inside the loop againStep Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
7 3
67
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
Add 1 to I
![Page 68: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/68.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 3 < 3 ?Step Number
Answer
I Number
Is I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
7 3
4 False68
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 69: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/69.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Back to the test: Is 3 < 3 ? Step
NumberAnswe
rI Numbe
rIs I < 3 ?
1 1
2 0
3 0
4 True
5 5
6 5
7 1
4 True
5 2
6 10
7 2
4 True
5 4
6 40
7 3
4 False69
Answer := 1I := 0Number := 0while (I is less than 3) begin
read(Number)Answer := Answer * NumberI := I + 1 end display Answer
1
2
3
4
5
6
7
![Page 70: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/70.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
That’s the End of the Trace Table.
70
Last line of Program: display Answer
40Output the contents of variable Answer
![Page 71: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/71.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java Versionint Answer;int Number;int I;
Answer = 1;I = 0;Number = 0;while (I < 3) { System.in.read(Number); Answer = Answer * Number; I = I + 1; }System.out.println(Answer);
71
![Page 72: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213 blakews@hope.ac.uk](https://reader030.vdocuments.net/reader030/viewer/2022032800/56649d2c5503460f94a02616/html5/thumbnails/72.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Next?• Assessment due – this has been emailed to
you.
if (!receivedAssessment)
begin
email [email protected]
end
else
begin
do assessment
end