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


Groups > comp.lang.c > #75453

Re: Shortened form of condition

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Shortened form of condition
Date 2015-11-09 14:44 +0000
Organization A noiseless patient Spider
Message-ID <87h9kvp6zb.fsf@bsb.me.uk> (permalink)
References <b6b2a9ec-4944-4e34-95e1-779161509469@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:

> int main (void)
> {
<snip>
>     bool end_of_text = false;
<snip>
>     while ( !end_of_text ) 
>     {
<snip>
>     }
<snip>
> }
> I have an issue with the shorted form of condition in while statement.
>
> Before the loop starts the bool value end_of_text is set to
> false.
<snip>
> The condition in the loop says: while (not end_of_text); 
>
> end_of_text was set to false initially, hence I assume 'not
> end_of_text' means that 'while end_of_text is true',

You need to break it down into parts -- there's the while statement and
the controlling expression and you need to treat them separately.  You
don't want to if "while ..." is true.  What matters is the value of the
expression, and the loop will execute provided that expression is 'true'.

The fact that (initially) end_of_text is false simply means that
!end_of_text is true.  In fact 'false', in C, is just another word for 0
and !end_of_text will have the value 1.  Another ! would flip the value
again making !!end_of_text 0.  Just to be sure, try it:

  #include <stdio.h>
  #include <stdbool.h>
   
  int main(void)
  {
       bool end_of_text = false;
       printf("eot=%d !eot=%d !!eot=%d\n",
              end_of_text, !end_of_text, !!end_of_text);
  }

> which (I understand it) is logically incorrect. I don't see how while loop
> works - why and when it starts and ends.

So when the loop is first encountered, the controlling expression has
the value 1 -- that's 'true' in C speak, so the body of the loop will be
entered.  Any time the body of the loop sets end_of_text to true (1),
the next time the controlling expression -- !end_of_text -- is evaluated
the result will be false (0) and the body won't be entered.

<snip>
-- 
Ben.

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


Thread

Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-09 06:23 -0800
  Re: Shortened form of condition Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-09 14:44 +0000
  Re: Shortened form of condition James Kuyper <jameskuyper@verizon.net> - 2015-11-09 10:17 -0500
  Re: Shortened form of condition Barry Schwarz <schwarzb@dqel.com> - 2015-11-09 09:20 -0800
  Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-09 10:11 -0800
    Re: Shortened form of condition Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-09 10:35 -0800
      Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-10 00:14 -0800
        Re: Shortened form of condition Barry Schwarz <schwarzb@dqel.com> - 2015-11-10 03:04 -0800
          Re: Shortened form of condition glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-10 21:42 +0000
        Re: Shortened form of condition Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-10 11:13 +0000
          Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-10 07:58 -0800
            Re: Shortened form of condition Richard Heathfield <rjh@cpax.org.uk> - 2015-11-10 17:06 +0000
            Re: Shortened form of condition James Kuyper <jameskuyper@verizon.net> - 2015-11-10 12:11 -0500
              Re: Shortened form of condition James Kuyper <jameskuyper@verizon.net> - 2015-11-10 14:01 -0500
          Re: Shortened form of condition Keith Thompson <kst-u@mib.org> - 2015-11-10 08:30 -0800
            Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-10 09:07 -0800
              Re: Shortened form of condition Keith Thompson <kst-u@mib.org> - 2015-11-10 09:29 -0800
                Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-10 11:02 -0800
                Re: Shortened form of condition Keith Thompson <kst-u@mib.org> - 2015-11-10 11:33 -0800
                Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-11 00:28 -0800
                Re: Shortened form of condition Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-11 16:05 +0000
                Re: Shortened form of condition glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-12 09:29 +0000
                Re: Shortened form of condition Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-12 10:56 +0000
                Re: Shortened form of condition Richard Heathfield <rjh@cpax.org.uk> - 2015-11-11 16:24 +0000
                Re: Shortened form of condition Keith Thompson <kst-u@mib.org> - 2015-11-11 08:53 -0800
                Re: Shortened form of condition Richard Heathfield <rjh@cpax.org.uk> - 2015-11-11 16:59 +0000
                Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-11 06:42 -0800
                Re: Shortened form of condition Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-10 14:04 -0500
              Re: Shortened form of condition Barry Schwarz <schwarzb@dqel.com> - 2015-11-10 10:43 -0800
    Re: Shortened form of condition James Kuyper <jameskuyper@verizon.net> - 2015-11-09 13:44 -0500
    Re: Shortened form of condition Keith Thompson <kst-u@mib.org> - 2015-11-09 10:57 -0800
      Re: Shortened form of condition Philip Lantz <prl@canterey.us> - 2015-11-10 01:52 -0800
    Re: Shortened form of condition Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-09 19:35 +0000
      Re: Shortened form of condition Keith Thompson <kst-u@mib.org> - 2015-11-09 11:52 -0800
  Re: Shortened form of condition Alla _ <modelling.data@gmail.com> - 2015-11-10 00:33 -0800
    Re: Shortened condition Richard Heathfield <rjh@cpax.org.uk> - 2015-11-10 09:00 +0000
  Re: Shortened form of condition Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-10 16:01 -0800

csiph-web