samedi 15 octobre 2016

IF statement overriding FOR loop in C

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;
}

Aucun commentaire:

Enregistrer un commentaire