Reputation: 13
I'm have trouble with a for loop. Basically, I get the output that I want, but for the wrong reason. This program prints out
xoxoxoxo
oxoxoxox
xoxoxoxo
oxoxoxox
xoxoxoxo
oxoxoxox
xoxoxoxo
oxoxoxox
The problem is the order in which this is produced. I'm required to have the inner loop iterate 8 times for every outer loop (also iterating 8 times), which isn't happening.
The compiler keeps returning to the if statement instead after printing, instead of returning to check the inner for loop (j).
#include <stdio.h>
int main() {
int i;
int j = 0;
for (i = 1; i < 9; i++) {
for (j = 1; j < i + 4 && j < 5; j++) {
if (i % 2 == 0) {
printf("o");
printf("x");
}
else {
printf("x");
printf("o");
}
}
printf("\n");
}
getchar();
return 0;
}
Upvotes: 1
Views: 179
Reputation: 1681
I assume you misinterpret the behavior of your debugger, and the code actually does exactly what it is supposed to do. Why your debugger does not jump to the head of the for loop but instead immediately to the if again, I cannot say. But they are all different about these things :)
Upvotes: 1