Assertions
Category: Assertions
Author: Marty Stepp
Book Chapter: 5.5
Problem: Assertions
For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false. (You may abbreviate them as A, N, or S.)
public static int count(int n) {
int even = 0;
int odd = 0;
<span class="assertionpoint">// Point A
while (n != 0 && even <= odd) {
if (n % 2 == 0) {
even++;
<span class="assertionpoint">// Point B
} else {
<span class="assertionpoint">// Point C
odd++;
}
n = n / 2;
<span class="assertionpoint">// Point D
}
<span class="assertionpoint">// Point E
return even - odd;
}
1) n == 0
2) even <= odd
3) n % 2 == 0