Boolean combination.

 

Today i encountered a problem but i have no idea why it's happen.

bool a,b,c;

..........//do stuff
..........
//now a,b,c are all true. I tested them:

if(a){
Alert("a");  
}
if(b){
Alert("b");      // All a,b,c true, and these three alert does appear.
}
if(c){
Alert("c");
}

But when I erased all three if statements above and replace them with

if(a && b && c){        //Alert "Stuff 1" doesn't appear even though a,b,c are all true.
Alert("Stuff 1");  
}
else if(b && c){
Alert("Stuff 2");      // Only this else if(b && c) execute and make Alert "Stuff 2".
}

Can someone please help me?

 

I executed the following code in the init() of an EA . . .

   bool a = true, b = true, c = true;
   if (a && b && c)
      Print ("Test Successful!");
   else
      Print ("Test Failed.");

. . . and it produced the following output into the journal/log:

15:17:08 TestingEA EURUSD,H1: loaded successfully

15:17:09 TestingEA EURUSD,H1: Test Successful!

15:17:09 TestingEA EURUSD,H1: initialized

15:17:21 TestingEA EURUSD,H1: deinitialized

15:17:21 TestingEA EURUSD,H1: uninit reason 1

15:17:21 TestingEA EURUSD,H1: removed

Are you sure your a, b, and c are actually set to true? Remember: if you declare a bool variable without assigning it an initial value (true or false), the variable will be initialized to false (0). See Initialization of Variables.
 

Yes. I'm sure they are set to true. Here is the code:

if(NewBar()){            //boolean a
Alert("a ");
}
if(r){                   //boolean b
Alert("b");
}
if(Rcount>=29){          //boolean c
Alert("c");
}
if(NewBar()&& r && Rcount>=29){
Alert("a b c");
}

when the conditions are satisfied, this is what i receive in the Alert box. The 4th if statement didn't execute.

 
Erik1:

Today i encountered a problem but i have no idea why it's happen.

But when I erased all three if statements above and replace them with

Can someone please help me?

Why are you doing a bitwise AND on a bool ?
 
RaptorUK:
Why are you doing a bitwise AND on a bool ?

Hmmm . . . looks to me like the OP is using a Logical AND (&&), rather than a bitwise AND (&).
 
Thirteen:
Hmmm . . . looks to me like the OP is using a Logical AND (&&), rather than a bitwise AND (&).
I could have sworn I saw a single & . . .
 

I had test all 4 seperately, and only the first three works. The combine one doesn't. I also did.

if(r && Rcount>=29){
Alert("b c");
}

This one pops up just fine. So i though it might be gotta do with the NewBar() one.

int count;
bool NewBar()          //return true when new bar or candle appear on the chart
{
if (count!=iBarShift(NULL,0,0))
{
count=iBarShift(NULL,0,0);
return(true);
}
return(false);
}

I had been staring at this one for so long and yet no clue.

 
Erik1:

I had test all 4 seperately, and only the first three works. The combine one doesn't. I also did.

This one pops up just fine. So i though it might be gotta do with the NewBar() one.

I had been staring at this one for so long and yet no clue.

What happens if you hit max bars on chart ?
 
I'm sorry. I don't quite get what you mean by hit max bars on chart.
 
Erik1:
I'm sorry. I don't quite get what you mean by hit max bars on chart.
If the number of bars on your chart = max bars on chart the number will not increase . . . doesn't your new bar check fail in that circumstance ?
 
The max bar in chart is 65000, and currently, the number of bars on my chart is 2249, i don't think that's the case RaptorUK.
Reason: