help needed with if- else cases

Discussion in 'C Programming' started by pereges, Apr 9, 2008.

1. peregesGuest

in my program i have a piece of code like this -

int left_count, right_count;
left_count = right_count = 0;
for all triangles
{

a = condition that x coordinates of all vertices are <= split.x
b = condition that x coordinates of all vertices that are >= split.x

if(a is true)
left_count++;
else
if(b is true)
right_count++;
else
if(!a && !b)
{
left_count++;
right_count++;
}

}

Now with the above code, I get left_count as 1200 and right_count 619

When I did not have the else if(!a && !b), I was getting 581 and 581
which is 1162 i.e. triangles which are only in left box and only in
right box . Not the ones that belong to both left and right boxes. So
that means triangles which are on both sides totals to about 38. I
believe 619 for right_count is correct but theres something fishy
about lefT_count which should also be 619 or so in my opinion. Prior
to changing the code in the above manner i had if cases like below -

if(a is true)
left_count++;

if(b is true)
right_count++;

else
{
left_count++;
right_count++;
}

And I was getting left_count and right_count as 1200 and 1200 which
was quite surprising to see.

**actual code **

for(i=0; i<(*kd)->maxtriangles; i++)
{

a = (*kd)->v[(*kd)->t.v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>t.v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t.v2].x <= (*kd)-
>split.x;

b = (*kd)->v[(*kd)->t.v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>t.v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t.v2].x <= (*kd)-
>split.x;

if(a)
left_count++;
if(b)
right_count++;
else
if(!a && !b)
{
left_count++;
right_count++;
}

}

pereges, Apr 9, 2008

2. Ben BacarisseGuest

pereges <> writes:
<snip>
> a = condition that x coordinates of all vertices are <= split.x
> b = condition that x coordinates of all vertices that are >= split.x
>
> if(a is true)
> left_count++;
> else
> if(b is true)
> right_count++;
> else
> if(!a && !b)
> {
> left_count++;
> right_count++;
> }

<snip>
> if(a is true)
> left_count++;
>
> if(b is true)
> right_count++;
>
> else
> {
> left_count++;
> right_count++;
> }
>
> And I was getting left_count and right_count as 1200 and 1200 which
> was quite surprising to see.

The two patterns are quite different. The "else" applies only to
nearest "if" so in your second case, when a is false right_count is
always incremented since the if ... else increments it in both arms
regardless of the value of b.

You don't need the !a && !b test in your first version because it is
guaranteed to be true at that point in the code:

if (a)
left_count++;
else if (b)
right_count++;
else {
left_count++;
right_count++;
}

is the same.

--
Ben.

Ben Bacarisse, Apr 9, 2008

3. Barry SchwarzGuest

On Wed, 9 Apr 2008 09:49:48 -0700 (PDT), pereges <>
wrote:

>in my program i have a piece of code like this -
>
>int left_count, right_count;
>left_count = right_count = 0;
>for all triangles
>{
>
>a = condition that x coordinates of all vertices are <= split.x
>b = condition that x coordinates of all vertices that are >= split.x
>
>if(a is true)
>left_count++;
>else

This else does not exist in either set of code below. Fortunately it
makes no difference (except for a pathological case) but you need to

>if(b is true)
>right_count++;
>else
>if(!a && !b)
>{
> left_count++;
> right_count++;
>}
>
>}
>
>Now with the above code, I get left_count as 1200 and right_count 619
>
>When I did not have the else if(!a && !b), I was getting 581 and 581
>which is 1162 i.e. triangles which are only in left box and only in
>right box . Not the ones that belong to both left and right boxes. So
>that means triangles which are on both sides totals to about 38. I
>believe 619 for right_count is correct but theres something fishy
>about lefT_count which should also be 619 or so in my opinion. Prior
>to changing the code in the above manner i had if cases like below -
>
>if(a is true)
>left_count++;
>
>if(b is true)
>right_count++;
>
>else
>{
> left_count++;
> right_count++;
>}
>
>And I was getting left_count and right_count as 1200 and 1200 which
>was quite surprising to see.

Given your description of what a and b represent, both should not be
true except for a pathological case I will ignore for the moment.
However, both can be false.

If a is true, b must be false. You increment left_count twice, once
in the first if and one in the else. You also increment right_count
once in the else. Let a be true A times.

If b is true, a must be false. You increment right_count only once in
the second if and never increment left_count. Let b be true B times.

If both a and b are false, your increment both left_ and right_count
in the else. Let this condition occur C times.

left_count must equal 2*A+C.
right_count must equal A+B+C

The only way left_count can equal right_count is if A == B.

>
>
>**actual code **
>
>for(i=0; i<(*kd)->maxtriangles; i++)
>{
>
> a = (*kd)->v[(*kd)->t.v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>>t.v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t.v2].x <= (*kd)-
>>split.x;

>
> b = (*kd)->v[(*kd)->t.v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>>t.v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t.v2].x <= (*kd)-
>>split.x;

And here is the error that forces A to be equal to B. Your
comparisons for b should be >=, not <=. One might also argue that the
comparisons should be < and > without the =.

>
> if(a)
> left_count++;
> if(b)
> right_count++;
> else
> if(!a && !b)
> {
> left_count++;
> right_count++;
> }

If you indented more than one space and did so consistently, tit would
be much easier for you to tell what went with what.

>
>}

Remove del for email

Barry Schwarz, Apr 10, 2008