Counting number of TRUE or FALSE within an IF statement

This is a discussion on Counting number of TRUE or FALSE within an IF statement within the C Programming forums, part of the General Programming Boards category; Hi, new to these forums, intermediate self taught programmer, I have a question that could be considered an advanced use ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.
    Last edited by CommonTater; 05-07-2011 at 07:08 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( (answer = ( (test1) + (test2) + (test3) )) )
    {
    }
    That should do it.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3

    Angry

    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Vinny99 View Post
    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.
    Quote Originally Posted by Vinny99 View Post
    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.
    Quote Originally Posted by Vinny99 View Post
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intended false statement is seen as true
    By 987654 in forum C++ Programming
    Replies: 7
    Last Post: 09-17-2010, 01:00 PM
  2. true or false
    By johngav in forum C Programming
    Replies: 4
    Last Post: 03-19-2009, 03:25 PM
  3. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 04:26 PM
  4. counting true's and false's from bools
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2007, 03:57 PM
  5. Help with True, False, NOT, AND, OR...
    By cgsarebeast in forum C++ Programming
    Replies: 22
    Last Post: 05-09-2006, 08:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21