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,

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Introduction to C Programming
Decisions If statements in C.
Types of selection structures
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
ITEC113 Algorithms and Programming Techniques
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Program Design and Development
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Flow of Control Part 1: Selection
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Python Conditionals chapter 5
Control Structures RepetitionorIterationorLooping Part I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Controlling Program Flow with Decision Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Chapter 4: Control Structures I (Selection)
CprE 185: Intro to Problem Solving (using C)
Chapter 4 The If…Then Statement
Making Choices with if Statements
PROGRAM CONTROL STRUCTURE
Introduction to Programming
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structure Senior Lecturer
Selection CIS 40 – Introduction to Programming in Python
If statement.
Outline Altering flow of control Boolean expressions
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Structured Program
1) C program development 2) Selection structure
3 Control Statements:.
Chapter 4 Selection.
Problem Solving Skill Area 305.1
Computer Science Core Concepts
Boolean Expressions to Make Comparisons
Programming Concepts and Database
Relational and Logical Operators
Chap 7. Advanced Control Statements in Java
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Relational and Logical Operators
REPETITION Why Repetition?
Looping and Repetition
Control Structures.
Presentation transcript:

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

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

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

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

A sample Dry Run table After execution First num Second num Result Line 2 Line 5 17 Line 7 24 Line 8 41

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

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

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” If (mark > 49) grade = “C” If (mark > 39) grade = “D” grade = ‘F’ In this alternative the psuedo code crawls across

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’ if number = 3 print ‘yellow’ endif Loop end

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…

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

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

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

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 > max) max = second Print message to screen saying (larger of the two numbers is max)

Test plan example Test number First Second Expected Result - max 1 18 12 2 10 15 3 17 4 5 A 20 Error because not integer 6 -10 -17