Dry Run - PowerPoint PPT Presentation

About This Presentation
Title:

Dry Run

Description:

We can do this by prompting the user with a question e.g imagine a user is ... Prompt user to enter a second integer (please enter a second number) ... – PowerPoint PPT presentation

Number of Views:178
Avg rating:3.0/5.0
Slides: 16
Provided by: Zab1
Category:
Tags: dry | prompting | run

less

Transcript and Presenter's Notes

Title: Dry Run


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, recording the valves
    of the variables at each stage
  • You can do this with a table

2
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 dont 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

3
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

4
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

5
A sample Dry Run table
6
Relational Operators
  • Give examples
  • lt less than
  • gt greater than
  • lt Less than or equal to altb
  • gt greater than or equal cgtd
  • equals
  • ! not equals 5!6
  • Boolean expression TRUE or FALSE

7
If then Else statements
  • If ltTESTgt
  • ltSTATEMENT 1 gt
  • Else
  • ltSTATEMENT 2 gt
  • ltTESTgt is a boolean expression
  • Is ltTEST? Has a false valve then ltSTATEMENT 2 gt
    is executed
  • Is ltTEST? Has a false valve then ltSTATEMENT 1 gt
    is executed
  • Note the indentation this makes it easier to
    read

8
Nesting Layout
  • To make your algorithm easier to read nest the
    statements
  • If (mark gt 69)
  • grade A
  • Else If (mark gt 59)
  • grade B
  • Else If (mark gt 49)
  • grade C
  • Else If (mark gt 39)
  • grade D
  • Else
  • grade F

If (mark gt 69) grade A Else If (mark
gt 59) grade B Else If (mark
gt 49) grade C Else
If (mark gt 39) grade
D Else grade
F In this alternative the psuedo code crawls
across
9
Nesting of if-else and while loops
  • Int number
  • Number 3
  • For loop 1 to 3
  • if number 1
  • print red
  • else
  • if number 2
  • print green
  • else
  • if number 3
  • print yellow
  • endif
  • Loop end

10
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
    continue
  • Continue user response
  • While (continue Y)

Loop Body
11
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 lt 10)
  • Where item number number of items to be
    added

12
Problem solving
  • Understanding the problem
  • The plan decide how to tackle problem -
    algorithms
  • Test and Evaluate dry run and test plans

13
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

14
  • 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 second
  • Int max
  • Sring str
  • Prompt user to enter an integer (please enter a
    number)
  • Store the number into variable first
  • Prompt user to enter a second integer (please
    enter a second number)
  • Store the second number into variable second
  • Max first
  • if (second gt max)
  • max second
  • Print message to screen saying (larger of the two
    numbers is max)

15
Test plan example
Write a Comment
User Comments (0)
About PowerShow.com