Boolean IF syntax

There looks to be different options for writing IF statements when testing boolean variables.
Are the following IF statements (respectively for X and Y) intechangeable?

   bool X = false;
   bool Y = false;

   if (X) {};
   if (X == true) {};
   if (X == 1) {};

   if (!Y) {};
   if (Y == false) {};
   if (Y == 0) {};

Do not use literals (0 nor 1)!

Basic convention says that 0 is false and either value (positive or negative) is true. What literal will be defined for false and true depends on language, standard version definition followed when make a compiler and how is defined internally.

Ok, using 1 and 0 is bad.

Are the following interchangeable?

if (X) {};
if (X == true) {};

if (!Y) {};
if (Y == false) {};

You can use both methods, but in practice is much clearer not to use true and false.

if (X) {...} means execute if X is true
if (!Y) {...} means execute if Y is NOT true, i.e, it is false

Thus yes, they are functional equivalents.

You can use both methods but if you use sensible names for the booleans then you can make the code more readable

if (theButtonIsPressed)
  {
    //do something
  }

Thanks.

noob314:
Do not use literals (0 nor 1)!

Basic convention says that 0 is false and either value (positive or negative) is true. What literal will be defined for false and true depends on language, standard version definition followed when make a compiler and how is defined internally.

When dealing with a bool value, false is 0 and true is 1. These are guaranteed by all C and C++ standards where bool is defined (C99 and later for C, not sure about C++). You simply cannot even have a bool that is neither 0 nor 1 (just try to assign a different value, say 2, to a bool variable and then print its value).

"When dealing with a bool value, false is 0 and true is 1. "

Should read:

When dealing with a bool value, false is 0 and true is not zero.
(At least in the Arduino).

I'm getting popcorn in.

christop:
When dealing with a bool value, false is 0 and true is 1.

It should be interprete as it is wrote already - Boolean and any other types of values are apples and oranges!

As 0 is treated as false and any other value as true in C like languages, it is possible following:

int value = 100;

while (value--) {
 // do something 100 times
}

Or this:

while (1) {
 // do something until device is powered
}

It is not possible to apply integer value in Boolean variable without automatic implicit conversion between types, which will be done exactly according basic convention, exactly if language that allow (this will not be possible with strict definition languages as for instance is Pascal). What is internally defined for true and false is not really important.

(I should point out, I don't like popcorn, but chewing it stops my jaw dropping when I read really stupid things)

TheMemberFormerlyKnownAsAWOL:
I'm getting popcorn in.

TheMemberFormerlyKnownAsAWOL:
(I should point out, I don't like popcorn, but chewing it stops my jaw dropping when I read really stupid things)

Someone definitely should stop you from trolling around...

So, if I write

bool theyAreEqual = ((1 +1) == 2);
Serial.println (theyAreEqual);

What would I see on the serial monitor?
Would it be:
a) "true"
b) "1"
c) "!0"

noob314:
Do not use literals (0 nor 1)!

Basic convention says that 0 is false and either value (positive or negative) is true. What literal will be defined for false and true depends on language, standard version definition followed when make a compiler and how is defined internally.

Basic conventions are:

  1. Any positive value above some agreed threshold level is TRUE.
  2. Any agreed value from zero to down is FALSE.

Data types exist for a reason. Using the appropriate data type means that you don't have to worry about conventions. A boolean variable can only have a value of true or false. There is no need to rely on conventions as to what values of other data types are equivalent to true and false.

For instance, if I set an unsigned int to a value of -1 will that be true or false ?

Predict the answer before you run this

void setup()
{
  Serial.begin(115200);
  unsigned int test = -1;
  Serial.println(test);
  if (test == true)
  {
    Serial.println("true");
  }
  else
  {
    Serial.println("false");
  }
}

void loop()
{
}

...which is one of the reasons I never compare against Boolean constants.

GolamMostafa:
Basic conventions are:

  1. Any positive value above some agreed threshold level is TRUE.
  2. Any agreed value from zero to down is FALSE.

If that is true, ask yourself why this will work as well as previous similar example?

int value = -100;

while (value++) {
 // do something 100 times
}

Here we talk about binary logic and progamming languages conventions, not electronic thresholds.

You have probably heard about ternary logic and ternary computers? Thus states is True (positive), Neutral (0, Ground) and False (Negative). First (and last, I believe) ternary computer was made in Russia in early 1970s. Ternary computers and ternary logic was simple and superior, but binary systems was easier to implement and they production where cost much less in these days...

I have not made these conventions, but computer pioneers. All about can be read in computer science books 101.

noob314:
If that is true, ask yourself why this will work as well as previous similar examplel?

int value = --100;

while (value++) {
// do something 100 times
}




Here we talk about binary logic and progamming languages conventions, not electronic thresholds.

You have probably heard about ternary logic and ternary computers? Thus states is True (positive), Neutral (0, Ground) and False (Negative). First (and last, I believe) ternary computer was made in Russia in early 1970s. Ternary computers and ternary logic was simple and superior, but binary systems was easier to implement and they production where cost much less in these days...

I have not made these conventions, but computer pioneers. All about can be read in computer science books 101.

--100 is 100, isn't it?

Have I failed or the Compiler is confused in the exam of Post#14?

I correctly reasoned that the value of the 'test' variable would be 65535, and it is proved correct by the sketch of Post#14; but, the other part is not what I have thought as per my Post#13.

Sketch of Post#14 (repeated)

void setup()
{
  Serial.begin(115200);
  unsigned int test = -1;
  Serial.println(test);
  if (test == true)
  {
    Serial.println("true");
  }
  else
  {
    Serial.println("false");
  }
}

void loop()
{
}

The value of 'test' variable is 65535.
@UKHeliBob is comparing 65535 with 1 as the symbolic data true is saved as 1.
As a result, Serial Monitor shows: false which is also correct.

Who is making the mistake -- @GolaMostafa, @UKHeliBob, or @Compiler?

GolamMostafa:
Who is making the mistake -- @GolaMostafa, @UKHeliBob, or @Compiler?

All upper. It is simply a nonsense comparing apples and oranges!

if (test == true) <- This is nonsense comparison, as: if (123 == "abc").
if (test) <- This will force compiler to do what it should, an implicit conversion regarding known convention

Types exist for a reason, as well as conventions and many languages standards when make proper compiler. Read history and you will know that earliest compilers produce executable code even source was full of syntax and semantic errors! What that code actually done was totally unpredictable if not simply freeze....

I'm really losing my time here - better for anyone here is simply to learn elementary programming stuff.