Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help needed with if- else cases

Reply
Thread Tools

help needed with if- else cases

 
 
pereges
Guest
Posts: n/a
 
      04-09-2008
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[i].v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>t[i].v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t[i].v2].x <= (*kd)-
>split.x;


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


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

}
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-09-2008
pereges <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-10-2008
On Wed, 9 Apr 2008 09:49:48 -0700 (PDT), pereges <(E-Mail Removed)>
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
be precise in your descriptions.

>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[i].v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>>t[i].v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t[i].v2].x <= (*kd)-
>>split.x;

>
> b = (*kd)->v[(*kd)->t[i].v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
>>t[i].v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t[i].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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
else if vs else { if A C++ 8 08-28-2010 08:55 PM
What's the use of the else in try/except/else? kj Python 15 05-23-2009 02:18 AM
for: else: - any practical uses for the else clause? metaperl.etc@gmail.com Python 25 09-30-2006 11:01 PM
Home server cases and external firewire cases thingy@nowhere.commy NZ Computing 5 03-14-2006 07:56 AM
ruby idiom for python's for/else while/else Gergely Kontra Ruby 16 09-20-2005 08:35 PM



Advertisments