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


Groups > comp.lang.c > #75455

Re: Shortened form of condition

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Shortened form of condition
Date 2015-11-09 10:17 -0500
Organization A noiseless patient Spider
Message-ID <n1qdan$sg$1@dont-email.me> (permalink)
References <b6b2a9ec-4944-4e34-95e1-779161509469@googlegroups.com>

Show all headers | View raw


On 11/09/2015 09:23 AM, Alla _ wrote:
...
>     bool end_of_text = false;
>     
>     printf ("Type in your text.\n");
>     printf ("When done, press 'RETURN'.\n\n");
>     
>     while ( !end_of_text ) 
>     {
>         read_line (text);
>         
>         if (text[0] == '\0')
>             end_of_text = true;
>         
>         else
>             total_words += count_words (text);
>     }
>     
...
> 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.
> 
> Read_line() function reads the input until the user enters '\n'
> (I am not posting this function here, because it is not relevant
> to my question).
> 
> 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',

Ben has already answered this question, and his answer is correct, but
I'll use a slightly different approach to giving you the same answer:


In C , true is 1 and false is 0, and all C operators that return logical
results return one of those values. In those contexts where C evaluates
a condition, 0 is treated as false, and non-zero is treated as true.
Therefore, while(condition) should be read in English as "while
condition compares unequal to 0". This particular statement  could be
read as "while 'not end_of_text' compares unequal to 0".  If end_of_text
is false, then "not end_of_text" is true, and therefore compares unequal
to zero; if end_of_text is true, then "not end_of_text" is false, which
compares equal to 0. Therefore, this is equivalent to saying "while
end_of_text is false".

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

Setting end_of_text to false ensures that the condition of the while is
true, thereby executing the body of the loop. It continues looping until
the line "end_of_text = true;" is executed, in which case "not
end_of_text" will be false, so the loop ends.
-- 
James Kuyper

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