Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.

Similar presentations


Presentation on theme: "1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne."— Presentation transcript:

1 1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

2 Chapter 3 22 Testing if Statements Quality assurance is the process of making sure software is developed to the highest standards given constraints of time and money. At minimum, test data should try to achieve complete code coverage. – When all lines of code are tested at least once. – Not the same as testing all logical paths. 2

3 Chapter 3 33 Testing if Statements (continued) Equivalence class: all the sets of test data that exercise a program in the same manner. Boundary conditions: test data that assesses a program’s behavior on or near the boundaries between equivalence classes. Extreme conditions: data at the limits of validity. Must also test data validation rules. – Enter values that are valid and invalid, and test the boundary values between the two. 3

4 Chapter 3 44 Nested if Statements Nested if statements are an alternative to using logical operators in a complex program. 4 An everyday example of nested ifs written in Javish

5 Chapter 3 55 Nested if Statements (continued) 5 Flowchart for reading a book, watching TV, or going for a walk

6 Chapter 3 66 Logical Errors in Nested if Statements Removing the Braces: Better to overuse braces than underuse. Braces and indentation can also help readability. Avoiding Nested if statements: Sometimes helps to avoid logical errors. Rewrite as a series of independent if statements. 6

7 Chapter 3 77 Nested Loops A nested loop is a loop inside another loop. Example: determining if a number is prime. – Nesting a for loop inside another for loop. – Compute all the primes between two limits. – To enter another pair of limits, enclose the code in yet another loop. 7

8 Chapter 3 88 Testing Loops Looping statements make it challenging to design good test data. Frequently, loops iterate zero, one, or more than one time depending on a program’s inputs. Combinatorial Explosion: When the behavior of different program parts affects other parts, include all options in testing. Multiplicative growth: number of parts * number of test data sets. 8

9 Chapter 3 99 Testing Loops (continued) 9 Robust Programs: A program that produces correct results when it has valid inputs is not good enough. Must test using invalid data. A program that tolerates and recovers from errors in input is robust.

10 Chapter 3 10 Loop Verification The process of guaranteeing that a loop performs its intended task, independent of testing. The assert Statement: Allows the programmer to evaluate a Boolean expression and halt the program with an error message if the expression’s value is false. If true, the program continues. 10

11 Chapter 3 11 Loop Verification (continued) 11 Assertions with Loops: Input assertions: state what should be true before a loop is entered. Output assertions: state what should be true when the loop is exited.

12 Chapter 3 12 Loop Verification (continued) 12 Invariant and Variant Assertions: Loop invariant: an assertion that exposes a relationship between variables that remains constant throughout all loop iterations. – True before the loop is entered, and after each pass. Loop variant: an assertion whose truth changes between the first and final execution of the loop. – Guarantees the loop is exited.

13 Chapter 3 13 Summary 13 Nested if statements are another way of expressing complex conditions. A nested if statement can be translated to an equivalent if statement that uses logical operators. An extended or multiway if statement expresses a choice among several mutually exclusive alternatives.

14 Chapter 3 14 Summary (continued) 14 Loops can be nested in other loops. Equivalence classes, boundary conditions, and extreme conditions are important features used in tests of control structures involving complex conditions. You can verify the correctness of a loop by using assertions, loop variants, and loop invariants.

15 Chapter 3 15


Download ppt "1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne."

Similar presentations


Ads by Google