Four boolean loop

Post Reply
icon
Trader
Posts: 18
Joined: Thu Apr 12, 2012 4:49 pm

Four boolean loop

Post by icon »

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
User avatar
tomele
Administrator
Posts: 1208
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

Four boolean loop

Post by tomele »

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
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
icon
Trader
Posts: 18
Joined: Thu Apr 12, 2012 4:49 pm

Four boolean loop

Post by icon »

Thanks a lot , i am humbled by your generosity
icon
Trader
Posts: 18
Joined: Thu Apr 12, 2012 4:49 pm

Four boolean loop

Post by icon »

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;
}

User avatar
tomele
Administrator
Posts: 1208
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

Four boolean loop

Post by tomele »

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

Cheers
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
Post Reply

Return to “Coders Hangout”