Introduction to Programming
Document Sample


COMP1681 / SE15
Today’s Learning Objectives
Introduction
to Programming
Meet do-while loops
Learning about tracing and debugging code
For you to learn more about techniques for planning the
Lecture 10 solution to a programming problem
For you to recognise the importance of writing clear,
Flow of Control: Loops (Part 2) readable, well-documented code
SE15: Loops(2) 10–2
Lecture Outline do-while Loops
int count = 0;
do-while loops do
Top tips for loops {
Example of a nested loop System.out.print(count + “,”);
Tracing code count++;
Debugging in Drjava } while (count <= 10);
Top tips for writing code
!warning: the loop body is always executed once!
SE15: Loops(2) 10–3 SE15: Loops(2) 10–4
Hints More torn up code
Watch out for extra semicolons with for loops
Reconstruct the following fragments of code to produce
for (i=0; i < 10; i++); the following output:
Avoid declaring variables inside loops 0 4
0 3 x++;
Avoid break statements if possible for ( int y = 4; y > 2; y--)
1 4
You cannot use do-while unless you are certain that 1 3
the loop can iterate at least once. 3 4 if(x == 1) System.out.println(x + " " + y);
If you have computation that changes some numeric 3 3
for ( int x = 0; x < 4; x++)
quantity by some equal amount each time, consider a
for statement public static void main (String [] args)
A while statement is always the safest class MultipleFors
SE15: Loops(2) 10–5 Head First Java, Sierra & Bates, O’Reilly SE15: Loops(2) 10–6
1
UML Activity Diagrams
Initial node
What does the following code do? (Flow diagrams)
class LoopTest Declare int i
{ for(int i = 0; i < 10; i++) Set i = 0
public static void main(String [] args)
{
{
int y = 7; System.out.println(i);
for(int x = 1; x < 8; x++) true Enter loop
{ } Is i < 10?
body
y++; System.out.println(“done”);
if(x > 4) false
System.out.print(++y + " ");
if(y > 14) Print the
{ value of i
System.out.println("x = " + x); Action node print “done”
break;
}
} Increment i
} Final node
} SE15: Loops(2) 10–7 SE15: Loops(2) 10–8
Top tips for writing programs Simplifying the Problem
C
Convert only from ° to °F
Put the scaffolding in place, then stop, think and plan
Perform a single conversion
If the problem seems complex, simplify it or break it
down into more manageable pieces
Steps become
C
1. Read temperature in ° from keyboard
Write down in English the steps you need to take
2. Calculate temperature in °F
For each of the main steps of your solution, write a 3. Output result of calculation to screen
comment in your main method
Use the comments to remind you what to do at each
point in the program
SE15: Loops(2) 10–9 SE15: Loops(2) 10–10
Commenting Your Code Top Tips
// Temperature conversion program
Adopt a good coding style
// Written by Nick Efford, 2005-10-18
Add a small amount of code at a time
public class Temperature {
Compile and run after each new addition of code
public static void main(String[] args) ¡
Fix errors before adding more code!
{
// Read temperature in Celsius
Use temporary println statements to test for correct
behaviour, or run in the debugger
// Convert to Fahrenheit
// Output result of calculation
}
}
SE15: Loops(2) 10–11 SE15: Loops(2) 10–12
2
Iteration 1
Writing Readable Programs (Pseudocode)
Use a good coding style Read a temperature in Celsius from the keyboard
Convert temperature from Celsius to Fahrenheit
¡
Descriptive names for classes, methods, variables…
Output Fahrenheit temperature to screen
¡
Sensible use of blank lines and indentation
¡
Consistency!
Use an appropriate level of commenting
¡
Derive them from your pseudocode
¡
‘Comment as you go’, don’t add them all at the end!
SE15: Loops(2) 10–13 SE15: Loops(2) 10–14
Iteration 1 Iteration 2
(UML) Initial node (Pseudocode)
Read temperature in Read a temperature from the keyboard
Celsius from keyboard Read temperature scale from the keyboard
If temperature scale starts with ‘C’ or ‘c’:
Convert temperature from Celsius to Fahrenheit
Output Fahrenheit temperature to screen
Convert temperature
from Celsius to Fahrenheit Otherwise if temperature scale starts with ‘F’ or ‘f’:
Action node Convert temperature from Fahrenheit to Celsius
Output Celsius temperature to screen
Otherwise:
Output Fahrenheit Print an error message on the screen
temperature to screen
Final node
SE15: Loops(2) 10–15 SE15: Loops(2) 10–16
Iteration 2 Iteration 3
(UML) (Pseudocode)
Read temperature in
Celsius from keyboard
Decision Repeat:
node Read a temperature from the keyboard
Guard Read temperature
Read temperature scale from the keyboard
condition scale from keyboard
If temperature scale starts with ‘C’ or ‘c’:
[ starts with C or c ] [ starts with F or f ] Convert temperature from Celsius to Fahrenheit
Output Fahrenheit temperature to screen
Otherwise if temperature scale starts with ‘F’ or ‘f’:
Convert temperature Print error Convert temperature Convert temperature from Fahrenheit to Celsius
from Celsius to Fahrenheit message from Fahrenheit to Celsius Output Celsius temperature to screen
Otherwise:
Output Fahrenheit Output Celsius Print an error message on the screen
temperature to screen temperature to screen Ask user whether another calculation is required
s
While user' response starts with ‘Y’ or ‘y’
SE15: Loops(2) 10–17 SE15: Loops(2) 10–18
3
Iteration 3
(UML) Read temperature Your Turn!
Read temp. scale
How would you simplify Coursework 1?
What would you attempt to do in your first iteration?
[ C or c ] [ F or f ]
What would the pseudocode / activity diagram look like?
Convert to °F Print error Convert to °C
and output message and output
Another conversion?
Merge node
[ Y or y ]
SE15: Loops(2) 10–19 SE15: Loops(2) 10–20
Iteration 1:
Read numbers, stopping at –1 Summary
Read a number from the keyboard
We have
While the number last read is not equal to –1:
Read another number from keyboard
¡
Looked at do-while loops
¡
Looked at tracing programs and debugging
¡
Seen how solutions can be expressed as pseudocode
int score = keyboard.nextInt();
while (score != -1) { Read number ¡
Looked at an alternative, graphical representation for
// Do stuff here... solutions: the UML activity diagram
score = keyboard.nextInt(); ¡
Emphasised the importance of writing readable code
Compare with –1
}
Read another [ not equal ]
number
[ equal ]
SE15: Loops(2) 10–21 SE15: Loops(2) 10–22
Follow-up Work
Reading from Savitch
¡
Section 3.3 (using pseudocode to specify loops)
¡
Section 2.4 (commenting and coding style)
ve
Apply what you' learned today to Assignment 1
Go to the SE15 Off-Site Resources web page and visit
How NOT to do a programming assignment
SE15: Loops(2) 10–23
4
Get documents about "