I couldn't understand why the following behavior happens in my code
First lets check if there exists number that dividable by 5
int count = 1;
while (count < 10) {
if (count%5 == 0) {
fprintf(stderr, FIND);
exit(1);
} else {
printf("Not Yet");
count += 1;
}
Normally, I'm expecting that it will print "not yet" four times before it print FIND
however, it actually print FIND immediately even first four number doesn't go to the if statement. There is no output of "Not Yet". The output is simply
FIND
Anyway, I try to debug by adding one more line within the if statement.
int count = 1;
while (count < 10) {
if (count%5 == 0) {
printf("%d\n", count);
fprintf(stderr, INVALID_LINE);
exit(1);
} else {
printf("Not Yet");
count += 1;
}
}
The output becomes
Not YetNot YetNot YetNot Yet5
FIND
I couldn't understand
First code, why the program execute if-statement even if the condition doesn't satisfied.
Second code, when one print line is added, the code actually behave what I've expected