Translation:
"After running the code below, what is the value of k?'
This question was from my friend's homework(we were senior highs). He didn't know how to deal with this, so he asked me and then I tried to test this on my computer(Debian 10, gcc-10.2.1
). The result is (A) , which means the value of k is 6. I reflected on the if
statement but still had no idea. It utterly made no sense - especially the syntax(who will even write code in that way?) . That's why I would like to ask this question. Perhaps that's a bug I haven't met before.
The C code:
#include <stdio.h>
int main() {
int i, j, k = 0;
for ( i = 0 ; i <= 2 ; ++i ) {
for ( j = 0 ; j <= 2 ; ++j ) {
if ( i != 1i != 2 && j != 1 ) {
k = k + j;
}
}
}
printf("%d\n", k);
return 0;
}
Why can the following if
statement work?
if ( i != 1i != 2 && j != 1 )
And what's the value of 1i
?