tuhu tuhu - 1 month ago 6
C++ Question

Boolean comparison says it is true but the if says it is false

sorry if this question too basic for you guys, but I have this code that make me confuse. I create a boolean comparison before the if and put the same comparison to the if condition but it doesn't get executed inside the if block.

#include<iostream> #include<cstdio>

using namespace std;

int main()
{
float s[2] = {0.24};
printf("Is this is true? (1 = yes, 0= false): %d", s[0] >= 0) && (s[0] < (1/7 + 1/14));

printf("\nBefore if: s[%d] %f\n", 0, s[0]);
if ((s[0] >= 0) && (s[0] < (1/7 + 1/14)))
{
s[0] = 10;
}

printf("\nAfter if: s[%d] %f\n", 0, s[0]);
return 0;
}

O_Z O_Z
Answer
 printf("Is this is true? (1 = yes, 0= false): %d", s[0] >= 0) && (s[0] < (1/7 + 1/14));

shoud be

printf("Is this is true? (1 = yes, 0= false): %d", s[0] >= 0 && (s[0] < (1/7 + 1/14)));

You do the printf of s[0] >= 0 and then you do the && on the printf result

Comments