Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #60520

Re: can this be done recursively

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: can this be done recursively
Date 2015-04-20 13:09 +0000
Organization Freie Universitaet Berlin
Message-ID <cpkc3cFf30tU1@mid.uni-berlin.de> (permalink)
References <b0319922-1b5e-48d2-8cc7-5cd1a5e866e6@googlegroups.com> <21b34a42-46a6-42c2-bda5-aa8c3486c1b1@googlegroups.com> <mh2p1j$s0d$1@dont-email.me>

Show all headers | View raw


Richard Heathfield <rjh@cpax.org.uk> wrote:
> On 20/04/15 12:30, G G wrote:

> <snip>

> >
> > i have a question. the while statement of the do while statement. why does this
> > work?
> >
> > i used
> > do
> > {
> > ...
> > } while( ( choiceToContinue != 1 ) || ( choiceToContinue != 2) )
> >
> > with the expectation that if the user enter anything other than 1 or 2 (integers) that
> > the loop would continue to ask the user if they would like to continue
> > or quit. but that did not work. i replaced the logical or with the logical
> > and, &&, and it works. why?

Another way to tackle this is applying a bit of Boolean algebra
to your condition:

  ( ( choiceToContinue != 1 ) || ( choiceToContinue != 2 ) )

can obviously also be written as

  ( ! ( choiceToContinue == 1 ) || ! ( choiceToContinue == 2 ) )

Now

    ( ! A || ! B )   is the same as   ( ! ( A && B ) )

which is often useful to remember when things get a bit complex
(the proof is left to the reader;-) so your condition can be
transformed into

  ( ! ( ( choiceToContinue == 1 ) && ( choiceToContinue == 2 ) ) )

Since 'choiceToContinue' can never be 1 and 2 at the same time
all the inner stuff is always false. With the negation in front
of it the whole condition is thus always true, so you never get
out of the loop.
                              Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 09:50 -0700
  Re: can this be done recursively Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-19 21:14 +0100
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:20 -0700
  Re: can this be done recursively Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-19 16:32 -0400
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:25 -0700
  Re: can this be done recursively Keith Thompson <kst-u@mib.org> - 2015-04-19 14:09 -0700
    Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-19 14:35 -0700
      Re: can this be done recursively jt@toerring.de (Jens Thoms Toerring) - 2015-04-19 22:57 +0000
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-20 04:30 -0700
    Re: can this be done recursively Richard Heathfield <rjh@cpax.org.uk> - 2015-04-20 12:49 +0100
      Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-20 05:28 -0700
      Re: can this be done recursively jt@toerring.de (Jens Thoms Toerring) - 2015-04-20 13:09 +0000
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-22 09:42 -0700
    Re: can this be done recursively Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-22 10:12 -0700
    Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 13:29 -0400
    Re: can this be done recursively Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-22 20:57 +0100
      Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 16:12 -0400
        Re: can this be done recursively Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-22 23:14 +0100
          Re: can this be done recursively James Kuyper <jameskuyper@verizon.net> - 2015-04-22 18:56 -0400
  Re: can this be done recursively G G <gdotone@gmail.com> - 2015-04-23 07:59 -0700

csiph-web