Multiple Returns: Am I wrong?

This is a discussion on Multiple Returns: Am I wrong? within the C Programming forums, part of the General Programming Boards category; I was doing another K&R question and again my idea was quite different but I thought I would run it ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    Multiple Returns: Am I wrong?

    I was doing another K&R question and again my idea was quite different but I thought I would run it by you bright people.
    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;
    }
    My idea.
    Code:
    int lower(int c)
    {
    	(c >= 'A' && c <= 'Z') ? return c + 'a' - 'A' : return c;
    }

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by deadhippo View Post
    I was doing another K&R question and again my idea was quite different but I thought I would run it by you bright people.
    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;
    }
    My idea.
    Code:
    int lower(int c)
    {
    	(c >= 'A' && c <= 'Z') ? return c + 'a' - 'A' : return c;
    }
    I assume you mean:

    Code:
         return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
    In which case - no, yours isn't incorrect, and they probably look the same when the compiler's compiled and optimized the code anyway. However, you might want to look at the difference and see why the ternary operator is so nice - It can be put into assignments/function calls and so on - it doesn't need to be 'on a line by itself'.

    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.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks for the reply and the advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. Multiple Definition Error
    By timmeh in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2009, 11:25 AM
  3. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  4. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  5. what's wrong with my newbie program??
    By insoolated in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2001, 08:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21