# Thread: Counting number of TRUE or FALSE within an IF statement

1. ## Counting number of TRUE or FALSE within an IF statement

Hi, new to these forums, intermediate self taught programmer, I have a question that could be considered an advanced use of the IF statement

Lets take the following example

if (a || b || c || d || e == 10)
then do something

In the above statement if any of 'a' 'b' 'c' 'd' 'e' are equal to 10, then the statement is TRUE. OK thats easy and straight forward.

However, what if more than one of 'a' 'b' 'c' 'd' 'e' are true, lets say a == 10 and c == 10, this statement again would still return TRUE, OK thats still easy and straight forward.

Now heres the trick i want to learn, I want to return TRUE only if a certain number of the inner variables meet the test, ie if two of the variables satisfy the test condition return TRUE otherwise return FALSE

Maybe something like this????

if ((a || b || c || d || e == 10) == 2)

ie if two of the variables == 10 then return TRUE otherwise return FALSE if 1 3 4 or all 5 are == 10,

I could certainly use individual IF statements for each parameter and count incrememmt each time one of them returns TRUE, but in the application i am developing this could mean quite extensive additional code and checking.

Is there a way of saying if 2 of the following 5 parameters meet the condition then return TRUE or FALSE. It could just as easily be if 3 or if 1 of the test variables, but the aim is to be able to stipulate for how many of the supplied variables is true or false

I hope this is clear(er than mud), many thanks for any help in advance

2. Your code won't work... You need to do it like this...
Code:
`if ((a == 10) || (b == 10) || (c == 10) || (d ==10) || (e == 10))`
The conditional test exits when the first TRUE evaluation is found so there's no way to know how many evaluated to true or false. Your version would exit on the... if( a ... part, so long as a != 0 and the rest of it would never be evaluated leaving you to ponder why NONE of the variables you thought you were testing equals 10.

Also note that what you had is an example of the many situations where "compiles does not mean works" and you end up spending a couple of days trying to figure out why it doesn't work...

I could certainly use individual IF statements for each parameter and count incrememmt each time one of them returns TRUE, but in the application i am developing this could mean quite extensive additional code and checking.
That's why the C gods gave us functions.

3. Code:
```if( (answer = ( (test1) + (test2) + (test3) )) )
{
}```
That should do it.

Quzah.

4. Hi thanks for the really quick reply, Yea i understand what you are saying, and yes, whilst the example solution code will compile as you say it will indeed not work. It was more just for illustration purpose.

What approach could you suggest to determine if X number of parameters in a multiparameter IF scenario are TRUE (or FALSE). To add context i have pasted a code snippet below.

This is part of an image processing line walking task, i wish to check if a focus pixel is ON (ie == 255), and if so, how many neighbour pixels are on. If more than one neighbour pixel is ON, then i am in the middle of a line. I only want to perform execution if i am at the end of a line (ie only one neighbour pixel is ON)
Code:
```if (thispixel[u-(t->_imageWidth-1)] 	||
thispixel[u+1]			                ||
thispixel[u-1]  		        	||
thispixel[u+(t->_imageWidth+1)] 	||
thispixel[u+t->_imageWidth] 		||
thispixel[u+(t->_imageWidth-1)]  == 255)
{
//find which neighbour is on
if (thispixel[u-(t->_imageWidth-1)] 	== 255 ) //NE
{
// compute trajectory
pixelcount++; 	//increment pixel length counter
u = u-(t->_imageWidth-1);
v=v-1;		//adjust position of focus pixel pointer
goto label;		//continue walking using new focus pixel
}
else if (thispixel[u+1]		== 255 ) //E
{
// compute trajectory
pixelcount++; 	//increment pixel length counter
u = u+1;		//adjust position of focus pixel pointer
goto label;		//continue walking using new focus pixel
}
else if (thispixel[u-1]		== 255 ) //W
{
// compute trajectory
pixelcount++; 	//increment pixel length counter
u = u-1;		//adjust position of focus pixel pointer
goto label;		//continue walking using new focus pixel
}
else if (thispixel[u+(t->_imageWidth+1)]== 255 ) //SE
{
pixelcount++;
u = u+(t->_imageWidth+1);
v=v+1;
goto label;
}
else if (thispixel[u+t->_imageWidth]	== 255 ) //S
{
pixelcount++;
u = u+t->_imageWidth;
v=v+1;
goto label;
}
else if (thispixel[u+(t->_imageWidth-1)]	== 255 ) //SW
{
pixelcount++;
u = u+(t->_imageWidth-1);
v=v+1;
goto label;
}
}```

5. Originally Posted by Vinny99
Hi thanks for the really quick reply, Yea i understand what you are saying, and yes, whilst the example solution code will compile as you say it will indeed not work. It was more just for illustration purpose.
Of course it will work.
Code:
`(if( (answer = (1 + 0 + 0 + 0 + 1)) )`
Two of your truth tests passed, and three failed. Answer = 2.
Originally Posted by Vinny99
What approach could you suggest to determine if X number of parameters in a multiparameter IF scenario are TRUE (or FALSE).
I already showed you what works.
Originally Posted by Vinny99
To add context i have pasted a code snippet below.
Code:
```if ((thispixel[u-(t->_imageWidth-1)] 	||
thispixel[u+1]			||
thispixel[u-1]  			||
thispixel[u+(t->_imageWidth+1)] 	||
thispixel[u+t->_imageWidth] 		||
thispixel[u+(t->_imageWidth-1)]  == 255)<2  )```
Pretty simple, just like I showed you:
Code:
```if ( (answer = (
(thispixel[u-(t->_imageWidth-1)]) +
(thispixel[u+1]) +
(thispixel[u-1]) +
(thispixel[u+(t->_imageWidth+1)]) +
(thispixel[u+t->_imageWidth]) +
(thispixel[u+(t->_imageWidth-1)]  == 255)
) ) < 2 )```
Assuming you mean "if there are less than 2 of these checks that pass..."

Quzah.

6. Quzah, i was replying to CommonTater whilst you posted your answer. To CommonTater, perhaps i did not best describe my query. Quzah == abs(Legend) Thanks for that, you have put me right on the right track i thinks as me shivers me timbers.