|
Testing and Debugging Conditionals
|
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
= instead of
==
== instead of
.equals() with reference types== with floating point
numbersAn Example:
if (seatLocationCode == 100)
{
// ...
}
else if (seatLocationCode == 200)
{
// ...
}
else if (seatLocationCode == 300)
{
// ...
}
else
{
// ...
}
Test cases: 50 100 150 200 300 1000
An Example:
if ((income > 0.0) && (income < 20000.0))
{
// ...
}
else
{
// ...
}
Test cases: -1.0 0.0 10000.0 20000.0 20001.0
An Example of a Compound Condition:
if ((height >= 62) && (sex == 'M'))
{
// ...
}
Test cases:
70, 'M'
70, 'F'
62, 'M'
62, 'F'
50, 'M'
50, 'F'
An Example of Nested Conditions:
if (height >= 62)
{
if (sex == 'M')
{
// ...
}
else
{
// ...
}
}
else
{
if (sex == 'M')
{
// ...
}
else
{
// ...
}
}
Test cases:
70, 'M'
70, 'F'
62, 'M'
62, 'F'
50, 'M'
50, 'F'
print() calls before, inside,
and after the conditional statementsprint() calls that check
for "accidental assignments" (i.e., use
of the = operator instead of the
== operator) // For debugging
JMUConsole.printf("Before: height(%d) sex(%c)", height, sex);
JMUConsole.printf(" (height >= 62) %s)",(height>=62));
JMUConsole.printf(" (sex == 'M') %s)",(sex=='M'));
if ((height >= 62) && (sex == 'M'))
{
// For debugging
JMUConsole.printf("In if-block: height(%d) sex(%c)", height, sex);
// ...
}
else
{
// For debugging
JMUConsole.printf("In else-block: height(%d) sex(%c)", height, sex);
// ...
}
// For debugging
JMUConsole.printf("After: height(%d) sex(%c)", height, sex);