hey guys i got this iece of code on a program i am writing, and i was jus twondering if you could help me to convert it so it uses the conditional operator to make it into a 1 line code,

cheers

int total, start, stop, increment;

if (total <= start)
    increment = 0;
else if (total >= start && total < stop)
   increment = total - start;
else
   increment = stop - start;

cheers again

Recommended Answers

All 6 Replies

Why bother, it looks perfectly readable as it is.

The ?: operator (you'll need two of them) will just render it an unreadable mess.

The code won't be any quicker for using ?:

hey guys i got this iece of code on a program i am writing, and i was jus twondering if you could help me to convert it so it uses the conditional operator to make it into a 1 line code,

cheers

int total, start, stop, increment;
 
if (total <= start)
    increment = 0;
else if (total >= start && total < stop)
   increment = total - start;
else
   increment = stop - start;

cheers again

Your can replace it by simple line
increament=total<=start ? 0 : (total >= start && total < stop) ? total - start : stop - start;

I agree with Salem -- it looks ok the way you have it. Putting it all on one line of code only saves band-width, paper and ink. Does nothing at all for the compiler or program speed.

perhaps, it is ok that way and knowing it the other way is also part of search for knowledge....

learn both.....


perhaps, it is ok that way and knowing it the other way is also part of search for knowledge....

learn both.....

if (total <= start)
    increment = 0;
else if (total >= start && total < stop)
   increment = total - start;
else
   increment = stop - start;

for readability

total<=start? increment = 0 : ((total >= start) && (total < stop))? increment =total -start : increment =stop - start ;

but i would prefer the code of ravi singhal

> but i would prefer the code of ravi singhal
Why?

Program execution speed has nothing to do with how many source code characters there are, or how many newlines you have.

Trading readability for imaginary (or highly marginal) gains is never a good idea IMO. Instead of being able to glance at the code and know what it's doing in just a few seconds, it now takes 10's of seconds to parse the line fully and figure out what's going on.

> also part of search for knowledge
Like understanding that there are places where ?: is really useful, and this isn't one of them.

If you think it makes you look 'leet', then think again. It might impress the noobs, but to a professional programmer, it's just a mess that you would be told to rewrite using something less obfuscated.

> also part of search for knowledge
Like understanding that there are places where ?: is really useful, and this isn't one of them.

If you think it makes you look 'leet', then think again. It might impress the noobs, but to a professional programmer, it's just a mess that you would be told to rewrite using something less obfuscated.

but no crime if you know it.....
not compulsory but fairly necessary..
bill gates would try to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.