Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #102217
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: while loop condition doesn't work |
| Date | 2017-01-20 12:19 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <871svxdiu9.fsf@bsb.me.uk> (permalink) |
| References | <750784a4-22dc-44ef-8972-34d4fb048b00@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> Please, help me to understand why a very simple while loop
> condition doesn't work - it doesn't stop the loop when I
> expect it to stop.
> Please, take a look at the piece of code, and my explanation
> of how I intended it to perform:
>
> Input:
> 1062,1252,1587,1934,2519
>
> Count starts at 5.
>
> double number_1 = strtod(ptr, ¬_converted);
> if (not_converted > ptr)
> {
> double number_2 = 0;
>
> while(not_converted > ptr && (!isspace(*not_converted)
> || *not_converted != '\0'))
> {
> ptr = not_converted + 1;
> number_2 = strtod(ptr, ¬_converted);
> count--;
> }
> if(count != 1)
> return false;
> }
> Explanation:
> (1) before the loop number_1 = 1062, and
> not_converted is ",1252,1587,1934,2519"
> (2) not_converted is bigger than ptr, and not_converted
> doesn't point to any space character or '\0';
That's not what your test does. Please, please, please, if you take
nothing else from my coaching, always negate a while condition to see
what it is you are trying to establish:
while(nc > ptr && (!isspace(*nc) || *nc != '\0')) ...
is trying to make the condition false. It stops when
!(nc > ptr && (!isspace(*nc) || *nc != '\0'))
== !(nc > ptr) || !(!isspace(*nc) || *nc != '\0')
== nc <= ptr || (!!isspace(*nc) && !(*nc != '\0'))
== nc <= ptr || (isspace(*nc) && *nc == '\0')
== nc <= ptr || 0
== nc <= ptr
I've urged you to do this on several occasions. Do it every time you
write a loop until you can do it without thinking. (It's complicated by
break, but you should be avoiding break.)
The main logic rules involved are called De Morgan's laws:
!(A && B) => !A || !B
!(A || B) => !A && !B
<snip>
On another issue, a well-structured program to read numbers and checking
how many there are will have a call to strtod in only one place.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 01:24 -0800
Re: while loop condition doesn't work mark.bluemel@gmail.com - 2017-01-20 03:29 -0800
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 12:19 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 05:12 -0800
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 13:17 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 05:20 -0800
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 15:07 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 05:31 -0800
Re: while loop condition doesn't work Manfred <noname@invalid.add> - 2017-01-20 15:23 +0100
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 15:16 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 07:45 -0800
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 16:06 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 08:37 -0800
Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 17:54 +0100
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 17:04 +0000
Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 19:08 +0100
Re: while loop condition doesn't work Tim Rentsch <txr@alumni.caltech.edu> - 2017-01-30 12:51 -0800
Re: while loop condition doesn't work Keith Thompson <kst-u@mib.org> - 2017-01-30 14:09 -0800
Re: while loop condition doesn't work supercat@casperkitty.com - 2017-01-30 14:57 -0800
Re: while loop condition doesn't work Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-01 21:31 -0800
Re: while loop condition doesn't work Keith Thompson <kst-u@mib.org> - 2017-02-02 10:01 -0800
Re: while loop condition doesn't work Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-04 00:22 -0800
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 19:12 +0000
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 10:54 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 23:22 -0800
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 11:02 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-21 01:22 -0800
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-21 11:00 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-21 08:35 -0800
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-21 17:17 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-21 09:32 -0800
Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 19:16 +0000
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 11:03 +0000
Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 14:34 +0100
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 07:40 -0800
Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 15:47 +0000
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 08:26 -0800
Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 18:01 +0100
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 09:26 -0800
Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 19:02 +0100
Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 10:13 -0800
Re: while loop condition doesn't work Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-21 05:36 -0800
csiph-web