Boolean and =

 

I have just spent hours trying to find an error in my code.

It turned out to be the simple case of using = where I should have put ==

I don't know if it is common knowledge or not, but the compiler did not pick up on the error.

In this example

bool a,b,c;
 a=false;
 b=false;
 c=false;
  
 if(a) 
    Print("a=true");
    else
    Print("a=false");
 if(b== true) 
    Print("b=true");
    else
    Print("b=false");
 if(c= true) 
    Print("c=true");
    else
    Print("c=false");
 if(c= false) 
    Print("c=false");
    else
    Print("c=true");

it will compile with no errors or warnings (editor build 887) and result in

04:52:08 aaa GBPUSD,H1: a=false

04:52:08 aaa GBPUSD,H1: b=false

04:52:08 aaa GBPUSD,H1: c=true

04:52:08 aaa GBPUSD,H1: c=true

I will have to get into the habit of using if(a)

At least with

 int d = 3;
 int e = 4;
 if(d=e)
    Print("3 DOES =4");

you will get a warning on compilation- expression not boolean

So it seems that a single = is expected with a boolean

04:52:08 aaa GBPUSD,H1: 3 DOES =4

I just thought that I would pass this on and possibly help others to avoid tearing their hair out if they make a similar error.

 
Good spot! That should be reported I reckon, compiler should give a warning.
 
ydrol:
Good spot! That should be reported I reckon, compiler should give a warning.

Why ? Which warning ? There is no warning needed.

 bool c;
 if(c= false) 
    Print("c=false");
    else
    Print("c=true");
You are affecting false to c, then the expression is evaluated, c is a boolean, so all is ok.
 

Hi, Almost every compiler for 'c' style languages that have '=' assignment and '==' comparison, will give a warning in this case to help programmers detect when they accidentally type '=' instead of '=='. Because 9 times out of 10 - '=' was not what was wanted...

I'm sure I could spend time finding many links to support :)

EDIT: Oops I might take that back - gcc didnt report it without additional switches! Nor perl - they all do with additional switches though. Its a 'nice to have' I guess!

 
ydrol:

Hi, Almost every compiler for 'c' style languages that have '=' assignment and '==' comparison, will give a warning in this case to help programmers detect when they accidentally type '=' instead of '=='. Because 9 times out of 10 - '=' was not what was wanted...

I'm sure I could spend time finding many links to support :)

I am waiting your links :-D
 
angevoyageur:

Why ? Which warning ? There is no warning needed.

You are affecting false to c, then the expression is evaluated, c is a boolean, so all is ok.


I see what you mean.

But it is an easy mistake to use = instead of == and for me at least, amazing how difficult to find my mistake.

As the compiler warns for things like implicit conversion of number to string etc, I think that a warning for this would be a good idea.

To be honest I can't think of any situation where I would want to use something like if(c= false). It seems to me to be basically the same as if(true), something else that I am unlikely to use.

 
GumRai:


. . .

To be honest I can't think of any situation where I would want to use something like if(c= false). It seems to me to be basically the same as if(true), something else that I am unlikely to use.


I try not to use the '==' inside of IF or WHILE operators. Rather, I try to use:

if (x)  // where x is TRUE
   ...

if (!x) // where x is FALSE
   ...

The above works well with boolean operations and operations of relation.

The compiler for build 509 would raise an illegal assignment error if the '=' was used inside the IF or WHILE operators. But, while I'm not sure I would ever use the assignment operator inside an IF operator, it is nice not to be constrained to refrain from doing so.

 
Thirteen:


I try not to use the '==' inside of IF or WHILE operators. Rather, I try to use:

The above works well with boolean operations and operations of relation.

The compiler for build 509 would raise an illegal assignment error if the '=' was used inside the IF or WHILE operators. But, while I'm not sure I would ever use the assignment operator inside an IF operator, it is nice not to be constrained to refrain from doing so.

Yes, as I said, I need to get used to changing the way that I do things.
GumRai:


I will have to get into the habit of using if(a)

Problem is that I tend to type as I am thinking. And as I am thinking if a = true, that is often what I will type.

Now that I am aware of this, I will know to check for these types of mistakes.

 

GumRai:

. . .

Problem is that I tend to type as I am thinking. And as I am thinking if a = true, that is often what I will type.

. . .

Rather than thinking if a equals true, I think of it as: if a is true or is not true. Maybe that will help you to not type the '='.
 
GumRai:


I see what you mean.

But it is an easy mistake to use = instead of == and for me at least, amazing how difficult to find my mistake.

As the compiler warns for things like implicit conversion of number to string etc, I think that a warning for this would be a good idea.

To be honest I can't think of any situation where I would want to use something like if(c= false). It seems to me to be basically the same as if(true), something else that I am unlikely to use.

I understand your point, but the problem is there is nothing to warn for the compiler. It's a logical error and the compiler can't detect logical error, if you want to sum a and b, and you wrote a-b do you want the compiler to warn you ? Would be awesome

For an example situation :

   bool condition;
   if(condition=buy conditions...)
     {
      //-- Send a buy order
     }
   else if(condition=sell conditions...)
     {
      //-- Send a sell order
     }
//-- further processing  
   ...

   if(!condition)
     {
      //-- No new order
     }
 

but the problem is there is nothing to warn for the compiler. It's a logical error and the compiler can't detect logical error


Many compilers will not warn by default, but will warn when using the most recommended switches for assisting with code quality (eg 'lint' )

So its not a bug, and not a big problem, because MQL4 doesnt mix Booleans and integer types, like C does. its more of a problem with languages that acccept 0 and !0 as false and true.