Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: if 'also' else statment

Reply
Thread Tools

Re: if 'also' else statment

 
 
William McNicol
Guest
Posts: n/a
 
      01-08-2010
Paul wrote:
> I sometimes think it would be nice if c had an 'also' statement,
> which meant that 'if a preceeding condition was true, do this as well'.
>
> This might save some untidy work-around, or much worse, copy-
> pasting. Let me know if you have a nice neat alternative.
>
> Here's an example of reading a book on weekends, but not weekdays.
> Naturally, there could be many more things done on a weekend.
>
> P.
>
> if (Day == Saturday)
> {
> WalkInPark();
> }
> else if (Day == Sunday)
> {
> AttendChurch();
> }
> also
> {
> ReadABook();
> }
> else
> {
> GoToWork();
> }


switch / case can be made to work:

switch (Day)
{
case Saturday: WalkInThePark () ;

if (0)
{
case Sunday : AttendChurch () ;
}
ReadABook () ;
break ;
default :
GoToWork () ;
break ;

}
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-08-2010
William McNicol <(E-Mail Removed)> writes:

> Paul wrote:
>> I sometimes think it would be nice if c had an 'also' statement,
>> which meant that 'if a preceeding condition was true, do this as well'.
>>
>> This might save some untidy work-around, or much worse, copy-
>> pasting. Let me know if you have a nice neat alternative.
>>
>> Here's an example of reading a book on weekends, but not weekdays.
>> Naturally, there could be many more things done on a weekend.
>>
>> P.
>>
>> if (Day == Saturday)
>> {
>> WalkInPark();
>> }
>> else if (Day == Sunday)
>> {
>> AttendChurch();
>> }
>> also
>> {
>> ReadABook();
>> }
>> else
>> {
>> GoToWork();
>> }

>
> switch / case can be made to work:
>
> switch (Day)
> {
> case Saturday: WalkInThePark () ;
>
> if (0)
> {
> case Sunday : AttendChurch () ;
> }
> ReadABook () ;
> break ;
> default :
> GoToWork () ;
> break ;
>
> }


Truly horrible (and, yes, I know you know it is!). I think I'd repeat
the ReadABook() function call, but if all repetition is to be avoided
and you need something that scales to more complex tests:

if (Day == Saturday)
WalkInPark();
else if (Day == Sunday)
AttendChurch();
else {
GoToWork();
goto day_over;
}
/* Day not over yet... */
ReadABook();
day_over:

If this were a function with nothing else to do, I'd use an early
return rather than the goto. So, having introspected, my internal
taste meter would prefer (in order):

(a) Small duplication of code other than a test/condition.
(b) Duplication of a simple condition.
(c) Early return.
(d) Goto.
(e) Flag setting and testing.

No idea why anyone would care what my taste would be, but Usenet
thrives on people saying what they think even if no one cares.

Of course, if the weekend test can be inverted, I'd probably never had
the issue:

if (weekday(Day))
GoToWork();
else {
if (Day == Saturday)
WalkInPark();
else AttendChurch();
ReadABook();
}

though, logically, this still has a duplicated test in it. I.e. I'll
bump (b) to the top of the list is I can hide the fact that I'd doing
it. I may need to think on that a bit more.

--
Ben.
 
Reply With Quote
 
 
 
 
Moi
Guest
Posts: n/a
 
      01-09-2010
On Fri, 08 Jan 2010 13:03:01 +0000, William McNicol wrote:

> Paul wrote:


> switch (Day)
> {
> case Saturday: WalkInThePark () ;
>
> if (0)
> {
> case Sunday : AttendChurch () ;
> }
> ReadABook () ;
> break ;
> default :
> GoToWork () ;
> break ;
>
> }


LOL!

AvK
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: if 'also' else statment Nobody C Programming 0 01-10-2010 07:01 AM
Re: if 'also' else statment TonyMc C Programming 1 01-08-2010 04:23 PM
PIX route inside statment John Cisco 1 09-27-2005 03:06 AM
Using "For Each ctrl In.." Statment Inside UserControl crjunk ASP .Net 2 09-03-2004 06:23 PM
Return statment JL ASP .Net 2 12-17-2003 10:47 AM



Advertisments