Testing and Debugging
Assert Condition
A checked condition can protect the line that depends on it.
Assert Condition
assert_condition.c
#include <stdio.h>
int main(void) {
int value = ;
int ok = value > 0;
if (ok) {
int quotient = 120 / value;
printf("ok=%d quotient=%d\n", ok, quotient);
} else {
printf("ok=%d quotient=skipped\n", ok);
}
return 0;
}
#include <stdio.h>
int main(void) {
int value = ;
int ok = value > 0;
if (ok) {
int quotient = 120 / value;
printf("ok=%d quotient=%d\n", ok, quotient);
} else {
printf("ok=%d quotient=skipped\n", ok);
}
return 0;
}
#include <stdio.h>
int main(void) {
int value = ;
int ok = value > 0;
if (ok) {
int quotient = 120 / value;
printf("ok=%d quotient=%d\n", ok, quotient);
} else {
printf("ok=%d quotient=skipped\n", ok);
}
return 0;
}
precondition
A precondition is a fact the next operation needs before it is safe.
checked path
Checking the condition first lets the program report the problem instead of using a bad value.