dimanche 13 janvier 2019

What is the difference between theses two types of code? I can't tell even thought they output different values

I was working a problem on a site (https://www.learn-c.org/en/While_loops) focused on teaching the basics in c. When I was supposed to solve this problem I thought it was easy and I had an answer immediately in mind. However, it turns out it was wrong.

This was my approach. It gave this output: 7 5 9 5 6

#include <stdio.h>

int main() {
int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
int i = 0;

while (i < 10) {
    i++;
    if(array[i]<5||array[i]>10)
        continue;

    printf("%d\n", array[i]);

}

return 0;
}

And this was the solution. which gave this output: 7 5 9 5

#include <stdio.h>

int main() {
int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
int i = 0;

while (i < 10) {
    if(array[i] < 5){
        i++;
        continue;
    }

    if(array[i] > 10){
        break;
    }

    printf("%d\n", array[i]);
    i++;
}

return 0;
}

I have looked everywhere I could think of to find an answer but I just can't seem to understand what the difference between my solution and the solution provided is.

Provided solution output:7 5 9 5

My solution output:7 5 9 5 6

Aucun commentaire:

Enregistrer un commentaire