stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Four boolean loop
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5680
Page 1 of 1
Author:  icon [ Sat Jun 15, 2019 9:43 am ]
Post subject:  Four boolean loop

Hello ,
I am trying to setup a loop with four boolean variables ,
To exit the loop I check a condition ( say sum > 20 ) if its true, I will exit .
This means I have to run the loop maximum 16 -1 ( 1 when all is false) times to represent the different combination of the boolean variables.
I wonder if you have a suggestion on how to setup such loop

Thanks in advance
Author:  tomele [ Sat Jun 15, 2019 1:05 pm ]
Post subject:  Four boolean loop

Hi

Code: Select all

for(int i=0; i<16; i++)
{
   cond1=(i & 1 > 0);
   cond2=(i & 2 > 0);
   cond3=(i & 4 > 0);
   cond4=(i & 8 > 0);
   
   ... your calculations

   if(sum>20) break;
}
Cheers
Author:  icon [ Sat Jun 15, 2019 3:29 pm ]
Post subject:  Four boolean loop

Thanks a lot , i am humbled by your generosity
Author:  icon [ Sat Jun 15, 2019 3:50 pm ]
Post subject:  Four boolean loop

Again thanks a lot tomele , I tried the code and it needed small changes to give the results, I am posting the changes if anyone ever wanted to do this

Code: Select all

for(int i=0; i<16; i++)
{
   bool cond1= ((i & 1) != 0);
   bool cond2= ((i & 2) != 0);
   bool cond3= ((i & 4) != 0);
   bool cond4= ((i & 8) != 0);
   Alert( " ..................................................");
   Alert( cond1 , " | " , cond2 , " | " , cond3 , " | " , cond4 ); 
   //.. your calculations
 
   //if(sum>20) break;
}

Author:  tomele [ Sat Jun 15, 2019 4:02 pm ]
Post subject:  Four boolean loop

Yes, my fault. I was just at posting the same. There were some parantheses missing.

Cheers
All times are UTC Page 1 of 1