Dry Run You can test your program without using a computer by dry running it on paper You act as the...

15
Dry Run • You can test your program without using a computer by dry running it on paper • You act as the computer – following the instructions of the program, recording the valves of the variables at each stage • You can do this with a table

Transcript of Dry Run You can test your program without using a computer by dry running it on paper You act as the...

Page 1: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Dry Run

• You can test your program without using a computer by dry running it on paper

• You act as the computer – following the instructions of the program, recording the valves of the variables at each stage

• You can do this with a table

Page 2: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Dry Run

• The table with have column headed with the names of the variables in the program

• Each row in the table will be labelled with a line number form the program.

• The entries of each row in th e table will be values of the variables after the execution of the statement on that line

• You don’t need a row for every line in the program, just those lines where some significant change to the state of the program occurs e.g a variable gets a new value

Page 3: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Dry Run

• In this table you can record all relevant changes to the variables as the program progresses, thereby test the logic of the program / algorithm

• Do a dry run before you code your program on computer this way any logic errors will come to light during the dry run

Page 4: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Example of a Dry run

• L1 Declare two variables , first num second num• L2 Initialise both variables to 0 • L3 first num = 0 second num = 0• L4 Ask user to enter first number• L5 Assign user input to first num variable • L6 Ask user to enter second number• L7 Assign user input to second num variable• L8 Add first num to second num• L9 Print result• Do a Dry run for this program assuming the user

enters 17 for first number and 24 for the second

Page 5: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

A sample Dry Run table

After

execution

First num Second num

Result

Line 2 0 0

Line 5 17 0

Line 7 17 24

Line 8 17 24 41

Page 6: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Relational Operators

• Give examples• < less than• > greater than• <= Less than or equal to a<=b• >= greater than or equal c>=d• == equals• != not equals 5!=6• Boolean expression – TRUE or FALSE

Page 7: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

If then Else statements

• If <TEST>• <STATEMENT 1 >• Else• <STATEMENT 2 >

• <TEST> is a boolean expression• Is <TEST? Has a false valve then <STATEMENT 2 > is executed• Is <TEST? Has a false valve then <STATEMENT 1 > is executed• Note the indentation – this makes it easier to read

Page 8: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Nesting Layout• To make your algorithm easier to read nest the statements

If (mark > 69) grade = “A”Else If (mark > 59) grade = “B”Else If (mark > 49) grade = “C”Else If (mark > 39) grade = “D”Else grade = ‘F’

If (mark > 69) grade = “A”Else If (mark > 59) grade = “B” Else If (mark > 49) grade = “C” Else If (mark > 39) grade = “D” Else grade = ‘F’In this alternative the psuedo code crawls across

Page 9: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Nesting of if-else and while loops

Int numberNumber =3For loop : 1 to 3

if number = 1 print ‘red’ else if number = 2 print ‘green’ else if number = 3 print ‘yellow’ endif

Loop end

Page 10: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

More Complex Loop Tests• Sometimes we may want to allow the user to terminate the

program/loop• We can do this by prompting the user with a question e.g

imagine a user is entering a series of numbers which will be added to together, like student marks. We can ask the user the question (Do you want to add another student mark? )

• We assign the user response to a variable named e.g. continue

Declare variable continue Initialise the variable Continue = ‘X’Ask the user (Do you want to add another student mark? )Assign/store the user response to variable continueContinue := user response

While (continue = ‘Y’)

Loop Body…

Page 11: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

More Complex Loop Tests

• Sometimes you may want to include more than one condition in your loop tests

• In such cases you can use boolean connectives (&& and ||)

• E.g.• while (continue = ‘y’) && (item number < 10)• Where item number = number of items to be added

Page 12: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Problem solving

• Understanding the problem

• The plan – decide how to tackle problem - algorithms

• Test and Evaluate – dry run and test plans

Page 13: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Test Plans

• Do before you code!

• Do a test plan for every branch test e.g. if the else and loop statements

• Extreme values such as 0 or negative numbers are tested in addition to ‘normal’ values. Testing the bounds is most efficient way for testing for run time errors

Page 14: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

• The following psuedo code / algorithm prints the larger of two numbers

• Do a test plan to find it – a series of valves to be input in variables first and second .Run the program several times

Int first Int secondInt maxSring strPrompt user to enter an integer (please enter a number)Store the number into variable firstPrompt user to enter a second integer (please enter a second number)Store the second number into variable secondMax = firstif (second > max) max = secondPrint message to screen saying (larger of the two numbers is max)

Page 15: Dry Run You can test your program without using a computer by dry running it on paper You act as the computer – following the instructions of the program,

Test plan exampleTest number

First Second Expected Result - max

1 18 12 18

2 10 15 15

3 17 17 17

4 0 0 0

5 A 20 Error because not integer

6 -10 -17 -10