Loop Equivalence Condition
Consider the following two Java methods, where integer arguments a and b are both greater than zero.
public static int countOne(int a, int b) {
int count = 0;
for (int j = 0; j < a / b; j++) {
count++;
}
return count;
}
public static int countTwo(int a, int b) {
int count = 0;
int index = 0;
while (index < a) {
count++;
index += b;
}
return count;
}
Under which condition will the countOne and countTwo methods return the same value?
A
Always, regardless of values of a and b
B
a % b is not zero but a / b rounds up
C
a % b must be zero
D
Never, they always produce different results
Question Leaderboard
Not enough data yet to show leaderboard.
