Chapter 3 Syntax, Errors, and Debugging

Sections 7.2 – 7.7
Nested Control Statements
Fundamentals of Java:
AP Computer Science
Essentials, 4th Edition
1
Lambert / Osborne
Testing if Statements

Chapter 3

2
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.
Testing if Statements (continued)

Chapter 3

3


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.
Nested if Statements
Chapter 3

4
Nested if statements are an alternative to using
logical operators in a complex program.
An everyday example of nested ifs
written in Javish
Nested if Statements (continued)
Chapter 3

5
Flowchart for
reading a book,
watching TV, or
going for a walk
Logical Errors in Nested if
Statements


Chapter 3

6



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.
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.
Chapter 3
–
7
Testing Loops


Chapter 3

8


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.
Testing Loops (continued)


Chapter 3

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.
Loop Verification

Chapter 3

10


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.
Loop Verification (continued)


Chapter 3

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.
Loop Verification (continued)


Invariant and Variant Assertions:
Loop invariant: an assertion that exposes a
relationship between variables that remains
constant throughout all loop iterations.
Chapter 3
–
12

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.
Summary

Chapter 3

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.
Summary (continued)

Chapter 3

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