Re: if 'also' else statment

Discussion in 'C Programming' started by William McNicol, Jan 8, 2010.

  1. 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 ;

    }
     
    William McNicol, Jan 8, 2010
    #1
    1. Advertising

  2. William McNicol <> 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.
     
    Ben Bacarisse, Jan 8, 2010
    #2
    1. Advertising

  3. William McNicol

    Moi Guest

    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
     
    Moi, Jan 9, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Adam

    "also" to balance "else" ?

    Ron Adam, Jun 14, 2005, in forum: Python
    Replies:
    27
    Views:
    792
    Tim Roberts
    Jun 17, 2005
  2. Replies:
    25
    Views:
    694
    MonkeeSage
    Oct 1, 2006
  3. kj
    Replies:
    15
    Views:
    591
    Lawrence D'Oliveiro
    May 23, 2009
  4. TonyMc

    Re: if 'also' else statment

    TonyMc, Jan 8, 2010, in forum: C Programming
    Replies:
    1
    Views:
    325
    Stefan Ram
    Jan 8, 2010
  5. Nobody

    Re: if 'also' else statment

    Nobody, Jan 10, 2010, in forum: C Programming
    Replies:
    0
    Views:
    410
    Nobody
    Jan 10, 2010
Loading...

Share This Page