C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2008, 12:04 AM   #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;
}
deadhippo is offline   Reply With Quote
Old 05-09-2008, 12:31 AM   #2
Ex scientia vera
 
Join Date: Sep 2007
Posts: 426
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.
IceDane is offline   Reply With Quote
Old 05-09-2008, 12:36 AM   #3
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
Thanks for the reply and the advice.
deadhippo is offline   Reply With Quote
Reply

Tags
conditional, if else, return

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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