![]() |
| | #1 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Multiple Returns: Am I wrong? The exercise is to rewrite an if else conditional using ?: I realize that I don't need parentheses around the conditional as ?: precedence is low but K&R says it is good practice. The solutions for this exercise that I have seen are slightly but significantly different using only one return at the beginning of the function and I understand those solutions but I was wondering if mine is wrong. Original: Code: int lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c + 'a' - 'A';
else
return c;
}
Code: int lower(int c)
{
(c >= 'A' && c <= 'Z') ? return c + 'a' - 'A' : return c;
}
|
| deadhippo is offline | |
| | #2 | |
| Ex scientia vera Join Date: Sep 2007
Posts: 426
| Quote:
Code: return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c; Although, you should be wary of using it too much, especially with complex if statements. They probably look exactly the same when compiled, but the ternary operator is often harder to read/dissect. | |
| IceDane is offline | |
| | #3 |
| Registered User Join Date: Feb 2008 Location: Yokohama
Posts: 48
| Thanks for the reply and the advice. |
| deadhippo is offline | |
![]() |
| Tags |
| conditional, if else, return |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Splitting source into multiple files(Linux & make) | IceDane | C Programming | 6 | 05-18-2009 07:31 AM |
| Multiple Definition Error | timmeh | C++ Programming | 9 | 02-15-2009 12:25 PM |
| Lame null append cause buffer to crash | cmoo | C Programming | 8 | 12-29-2008 03:27 AM |
| why Multiple define error ... | nilathinesh | C Programming | 2 | 10-19-2006 06:31 AM |
| what's wrong with my newbie program?? | insoolated | C++ Programming | 1 | 09-14-2001 08:49 PM |