For Loop with Conditional Logic
What is printed when the following code segment is executed?
public class LoopTest {
public static void main(String[] args) {
int count = 0;
for(int i = 0; i < 5; i++) {
if(i % 2 == 0)
count += i;
else if(i % 2 != 0)
count -= i;
}
System.out.println(count);
}
}
A
4
B
-2
C
2
D
0
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE