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. Advertisements

  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. Advertisements

  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. Advertisements

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. JL

    Return statment

    JL, Dec 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    502
    Hermit Dave
    Dec 17, 2003
  2. crjunk
    Replies:
    2
    Views:
    1,780
    crjunk
    Sep 3, 2004
  3. Ron Adam

    "also" to balance "else" ?

    Ron Adam, Jun 14, 2005, in forum: Python
    Replies:
    27
    Views:
    954
    Tim Roberts
    Jun 17, 2005
  4. Replies:
    25
    Views:
    1,007
    MonkeeSage
    Oct 1, 2006
  5. kj
    Replies:
    15
    Views:
    823
    Lawrence D'Oliveiro
    May 23, 2009
  6. TonyMc

    Re: if 'also' else statment

    TonyMc, Jan 8, 2010, in forum: C Programming
    Replies:
    1
    Views:
    407
    Stefan Ram
    Jan 8, 2010
  7. A
    Replies:
    8
    Views:
    1,088
    James Kanze
    Aug 28, 2010
  8. Gergely Kontra
    Replies:
    16
    Views:
    437
    Devin Mullins
    Sep 20, 2005
Loading...