Home


Pecobians.com
Excellence is our Patent - Try Pirating!
Just A Small C Question [JustASC] 2
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
Whats the most concise way you can replace an if statement of the sort:

if(a==b)
{
c=d;
}
with. Remember, there is no else part here...
Hint: The solution lies in a very basic property of C compilers...
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
Whats the matter guys...no replies today???
atleast try....
Regular Member
Regular Member

Joined:25 May 2005
Posts:20
                
Status: Offline
is it by DO WHILE and if not then plz tell the answer on my private message
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
@rahul: the answer that i have in mind doesnt have anything to do with a do while statement, neither can i think of a way with which i can make a more conciser solution using do while...Please share with us what u exactly had in mind cuz I may be wrong and that'll make me learn too...

Neways, the answer that i have will be put on the forum in a day or two, so can't PM u right now...just wait a bit...
Moderator
Moderator

Joined:17 Mar 2005
Posts:164
Location:New Delhi
                
Status: Offline
Don't know dude...
Only thing I can think of is to remove the braces..
if(a== b) c=d;
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
well, i'll give you a hint...
The solution will convert the if block into a single statement. This is true for ur answer too, navjot, but only when there is a single statement inside the braces. The solution that i've in mind will work for multiple statements inside the braces too, and convert the whole block into a single statement...(BTW, the "if" keyword won't be there in that solution, that much i can tell u.)
Groupie
Groupie

Joined:09 Jul 2005
Posts:376
Location:on road to glory
                
Status: Offline
maybe something of sort

( a==b ? c ? x)

plz make clear that can we assume a value in else case
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
@whacko: I have already mentioned it there should is "no" else statement, so you can't assume anything for that..
Groupie
Groupie

Joined:09 Jul 2005
Posts:376
Location:on road to glory
                
Status: Offline
using the macros

#define (a==b) (c=d);
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
@whacko: I cudnt understand how ur last solution wud achieve the purpose...
Neways, the solution has to support multiple lines withing the if block too..
Moderator
Moderator

Joined:14 Feb 2005
Posts:370
Location:College Park, MD
                
Status: Offline
OK, so I just saw this and I have some comments and some questions.

1) Are you asking us what the compiler *could* do to optimize this code block or what it definitely *will* do?

2) Are you sure we don't need to know any other information about the relationships between a,b,c and d?

In any case, here are a few comments:

* If the compiler can figure out earlier in the program (by constant folding, CSE, SSA and other techniques) that a will *never* be equal to b, then it can do what's called "dead code elimination" and basically remove that entire block (since it's dead code) but this doesn't seem like what you want.

* Hmm ... if you don't have any information about whether the boolean expression (a==b) evaluates to True or False, then I can't think of what sort of optimizations you can apply at compilation time.

If you have given us all the information that we needed to know, then I am stumped ! Smile
Privileged Member
Privileged Member

Joined:11 Feb 2005
Posts:521
Location:In front of the Screen!!!
                
Status: Offline
c=a=b?d:c;
not my answer.. one of my cubicle mate saw t his post and said this will be the reply!!!!
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
@wicked_sunny:
sorry, wrong answer on four accounts:
1. This statements will always give a true result and c will always be equal to d.
It should be written as c=(a==b)?d:c;

2. Even then, it includes an "else" part (":c") which i have specified in the problem that it shouldn't be there...

3. It is only true for the simple case of an assignment statement present within the if block (which i gave as an example) while i've specified that the block can have any type of arithmetic statements.

4. It can't be used for multiple statements within the if block....
Moderator
Moderator

Joined:14 Feb 2005
Posts:370
Location:College Park, MD
                
Status: Offline
Oh you have to got to be kidding me !! That's all you wanted ... boy, do I feel dumb !! I think the answer should be this:

(a==b) && (c=d);

The first boolean subexpression short-circuits the entire thing if it's not true and therefore 'c=d' is never evaluated unless a is equal to b.
Moderator
Moderator

Joined:11 Feb 2005
Posts:1086
Location:Gurgaon
                
Status: Offline
@nitin: Yupz u got it right once again...simple, isn't it?? Smile
and the beauty is that u can chain any no. of expressions to convert the whole block into a single statement...

like

(a==b) && (c=d) && (e=f+g) && (h=i*4); etc etc....

Since after the first condition (u can even chain conditions), all the expressions evaluate to true so all of them will be evaluated...

New question soon...
All times are GMT + 5.5 Hours  
Page 1 of 2