06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques.

Slides:



Advertisements
Similar presentations
Logic & program control part 3: Compound selection structures.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
05 Simple selection1June Simple selection CE : Fundamental Programming Techniques.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
07 Further testing1June Further selection CE : Fundamental Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter Making Decisions 4. Relational Operators 4.1.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Control Statements: Part1  if, if…else, switch 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Chapter 4: Control Structures I
Chapter 4: Making Decisions.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Making Decisions.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Control Structures I
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

06 Testing selection1June Testing selection CE : Fundamental Programming Techniques

06 Testing selection2June 15 Objectives In this session, we will: look at testing selections develop test plans and see how to document tests see how if statements are combined into more complex structures explain the importance of braces in selection statements

06 Testing selection3June 15 Testing when running programs that accept data we must check results produced by the code not sufficient to assume that if the program outputs some results, those results are correct testing is the process of finding errors debugging is the process of removing errors

06 Testing selection4June 15 Testing selections selections provide choice in the code different code is executed dependent on the data must execute every possible path through a program test plan must be designed choose values to ensure all possible paths are executed need to check for values around boundaries with discrete values it is necessary to try each possible value in a separate test

06 Testing selection5June 15 Testing simple selections 2 paths through program to check bank balance: must choose data that tests each path: Path 1: balance = Path 2: balance = //output message giving status of account if ( balance < 0.0 ) { System.out.println("Overdrawn"); } else { System.out.println("In credit"); } Path 1: executed if condition true Path 2: executed if condition false

06 Testing selection6June 15 Testing around boundaries errors may occur if the incorrect comparison operator is chosen: less than instead of less than or equal (< or <=) need to check for values around boundaries must choose data that tests around boundary above boundary: balance = 0.0 below boundary: balance = //output message giving status of account if ( balance < 0.0 )... Boundary between 0.0 and negative value

06 Testing selection7June 15 Testing expressions each comparison in a logical expression needs to be tested must choose data to test each comparison: age >= 18 age < 18 age <= 35 age > 35 //check whether person can go on 18 – 35 holiday if (( age >= 18 ) && (age <= 35))... Need to check all combinations

06 Testing selection8June 15 Testing complex conditions need to test each combination of each test must choose data to test each combination: true && true: number = 15 true && false: number = 3 false && true: number = 5 false && false: number = 4 //check whether number is divisible by 3 and 5 if (( number % 3 == 0 ) && (number % 5 == 0))...

06 Testing selection9June 15 Selecting data need minimal set of data that will test: all possible paths through the code all comparisons in expressions all possible combinations in logical expressions all boundary conditions must also calculate expected results for comparison against actual results program needs to be run using selected data

06 Testing selection10June 15 Documenting testing not sufficient to just run the program with carefully selected data test plan should show: the data to be used the reason for the test the expected result the actual result if actual results are not as expected: program will probably need to be corrected all tests must be rerun

06 Testing selection11June 15 Test plan for bank balance program DataReason for testExpected results Actual results balance = 57typical credit balance "In credit"As expected balance = -43typical overdrawn value "Overdrawn"As expected balance = 0lowest value that isn’t overdrawn "In credit"As expected balance = -0.01smallest overdrawn balance "Overdrawn"As expected

06 Testing selection12June 15 Multiple selection used to select one of several discrete groups only one alternative can be selected as soon as any condition is true, the statements in the associated block are executed after executing block, control passes to the end of the if statement

06 Testing selection13June 15 Multiple selection example – Grades problem: output a grade based on the exam result entered: over 70 is A 60 – 69 is B 50 – 59 is C 40 – 49 is D under 40 is E

06 Testing selection14June 15 Grades analysis what data is used? percentage: integer in the range 0 – 100 input by user need selection as different grade output dependent on percentage what operations are done before the selection? create Scanner prompt user for percentage input percentage what operations are done if percentage 70 or over? output "A" what operations are done if percentage ? output "B" what operations are done if percentage ? output "C what operations are done if percentage ? output "D" what operations are done if percentage is none of the above? output " what operations are done after the selection? none

06 Testing selection15June 15 //output grade based on percentage Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter percentage: "); int percentage = myKeyboard.nextInt(); if ( percentage >= 70 ) { System.out.println("A"); } else if ( percentage >= 60 ) { System.out.println("B"); } else if ( percentage >= 50 ) { System.out.println("C"); } else if ( percentage >= 40 ) { System.out.println("D"); } else { System.out.println("E"); } executed if condition 1 true executed if condition 1 false and condition 2 true executed if condition 1 and 2 false and condition 3 true executed if condition 1 2 and 3 false and condition 4 true executed if all conditions false Grades.java

06 Testing selection16June 15 Testing multiple selections must check each path through multiple selection DataReason for testExpectedActual percentage = 89typical value for "A""A"As expected percentage = 70lowest value for "A""A"As expected percentage = 69highest value for "B""B"As expected percentage = 60lowest value for "B""B"As expected percentage = 59highest value for "B""C"As expected percentage = 50lowest value for "B""C"As expected percentage = 49highest value for "B""D"As expected percentage = 40lowest value for "B""D"As expected percentage = 28typical value for "E""E"As expected

06 Testing selection17June 15 Nested selections actions to be performed within a selection can be anything, including other selections nested if statements are if statements within if statements the outer if is evaluated first if true, any inner if is evaluated

06 Testing selection18June 15 Nested selection example – Pension problem: determine the user’s eligibility for pension based on their sex and age: males are eligible for pensions from 65 females are eligible for pensions from 60

06 Testing selection19June 15 Pension analysis what data is used? gender: string input by user, either "male" or "female" age: positive integer input by user need selection as males and females dealt with in different ways what operations are done before the male / female selection? create Scanner prompt user for gender and age input gender and age what operations are done if gender is male? need nested selection as males 65 or over are treated differently what operations are done before the age selection? none what operations are done if age 65 or over? output "eligible male" what operations are done if age not 65 or over? output "ineligible male" what operations are done after the age selection? none

06 Testing selection20June 15 Pension analysis continued what operations are done if gender is not male? need nested selection as females 60 or over are treated differently what operations are done before the age selection? none what operations are done if age 60 or over? output "eligible female" what operations are done if age not 60 or over? output "ineligible female" what operations are done after the age selection? none what operations are done after the male / female selection? none

06 Testing selection21June 15 Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter gender: "); String gender = myKeyboard.next(); System.out.print("Enter age: "); int age = myKeyboard.nextInt(); if (gender.equals("Male")) { //check male's age if (age >= 65) { System.out.println("Eligible male"); } else { System.out.println("Ineligible male"); } else { //check female's age if (age >= 60) { System.out.println("Eligible female"); } else { System.out.println("Ineligible female"); } gender tested in outer if statement male age tested in inner if statement female age tested in inner if statement Pension.java

06 Testing selection22June 15 Testing nested selections must check each path through nested selection also need to check boundaries for each comparison male 65, male 64, female 60, female 59 DataReasonExpectedActual gender = "male" age = 37 typical value for ineligible male "Ineligible male" As expected gender = "male" age = 85 typical value for eligible male "Eligible male" As expected gender = "female" age = 43 typical value for ineligible female "Ineligible female" As expected gender = "female" age = 74 typical value for eligible female "Eligible female" As expected

06 Testing selection23June 15 Braces in if statements braces are not mandatory in if statements but... what is output for: a = 3, b = 3, c = 3? a = 3, b = 4, c = 3? if (a == b) if (a > c) System.out.println("a bigger than c"); else System.out.println("a not equal to b");

06 Testing selection24June 15 Matching else else matches the nearest preceding unmatched if in the same block braces used to block statements: if (a == b) { if (a > c) { System.out.println("a bigger than c"); } else { System.out.println("a not equal to b"); }

06 Testing selection25June 15 Summary In this session we have: tested selections constructed test plans combined if statements to form multiple and nested selections seen how braces affect if statements In the next session we will: look at further selection using the Switch statement