What is the conditional in this WHILE loop?? 
Author Message
 What is the conditional in this WHILE loop??

OK.  I'm stumped.

This (not so) simple while loop copies one buffer to another.   This
loop works, but what is the condition on which the WHILE loop exits??

register char *src;
char *dst;

while (*dst++ = *src++) {
  }

Assume error checking has occurred, and that there is ample room in the
"dst" buffer to to hold all of chars in the "src" buffer.

--
///\\\///\///\\///\\\In the end, we will conserve only what we love and
\\\ Cary Penniman ///respect.  We will love only what we understand. We
/// Goleta, CA    \\\will only understand what we are taught or allowed
\\\///\\\/\\\//\\\///to experience - Baba Dioum:African Conservationist



Sat, 02 Nov 2002 03:00:00 GMT  
 What is the conditional in this WHILE loop??

Quote:

> This (not so) simple while loop copies one buffer to another.   This
> loop works, but what is the condition on which the WHILE loop exits??

> while (*dst++ = *src++) {
>   }

This is equivalent to
        while ((*dst++ = *src++) != 0);
Do you see it now?  Keep in mind that the value of an assignment
is the value that was put into the target of the assignment.


Sat, 02 Nov 2002 03:00:00 GMT  
 What is the conditional in this WHILE loop??

Quote:

>This (not so) simple while loop copies one buffer to another.   This
>loop works, but what is the condition on which the WHILE loop exits??
>while (*dst++ = *src++) {
>  }

in c, every expression has a value.  even something as simple as

  x = y + 5;

in the case of assignments, the value of the expression is the value
that was assigned.  so in the above expression, the expression as a
whole has a value of x.

while loops will exit on a false value.  in c, false is zero, and
everything else is true.

therefore, the while loop will exit when a value of zero is assigned
to *dst.

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board




Sat, 02 Nov 2002 03:00:00 GMT  
 What is the conditional in this WHILE loop??


Quote:
> OK.  I'm stumped.

> This (not so) simple while loop copies one buffer to another.   This
> loop works, but what is the condition on which the WHILE loop exits??

> register char *src;
> char *dst;

> while (*dst++ = *src++) {
>   }

> Assume error checking has occurred, and that there is ample room in the
> "dst" buffer to to hold all of chars in the "src" buffer.

It will copy the char pointed to by src to the destination dst and, if the
value is a terminating '\0', the while fails and the loop exits.  The copy
happens before the test, so the terminator will be copied.  The value of an
assignment statement is the value received by the lvalue. Of course, that
value can be captured and made to do something within a more complex
statement:

    char *p;
    char *Msg = "This is a Test!";
    int n = 'a';
    if((p = strchr(Msg, n) != NULL)
    {
        /* p points to the first occurance of 'a' */
    }

The call to strchr(...) returns a pointer, that value is assigned to a
declared pointer varaible and that value is compared to NULL.  Note the
brackets to enforce the precedence; honestly, I don't know whether they are
necessary (I bracket like crazy sometimes), but it ensures that the last
operation is the pointer comparison with NULL.

Of course, going to extremes in writing complex statements can lead to some
hellish code to maintain.

--
Ross Driedger


"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."

-- Gene Spafford, 1992



Sat, 02 Nov 2002 03:00:00 GMT  
 What is the conditional in this WHILE loop??
: OK.  I'm stumped.

: This (not so) simple while loop copies one buffer to another.   This
: loop works, but what is the condition on which the WHILE loop exits??
: register char *src;
: char *dst;

: while (*dst++ = *src++) {
:   }
: Assume error checking has occurred, and that there is ample room in the
: "dst" buffer to to hold all of chars in the "src" buffer.

Assuming properly nul ('\0') terminated C strings, the while() loop will
break when the '\0' character is reached.  '\0' like 0 evaluates to false
while != 0 evaluates to true.

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Sat, 02 Nov 2002 03:00:00 GMT  
 What is the conditional in this WHILE loop??
Thanks to all who answered!

You confirmed my suspisions.  I think the final answer is...
======================================
register char *src;
char *dst;
char value;

value = (*dst=*src);         /*  value evaluates to the value that is
assigned to *dst */

while (value != '\0') {       /* '\0' is the condition on which the while
loop exits.  NOTE: value != 0 works as well */
    dst++;
    src++;                        /* increment pointers afterwards */
    value = (*dst=*src);

Quote:
}

======================================

Although the code...

while (*dst++ = *src++) {
  }

... is more concise. The code...

while ((*dst++ = *src++) != '\0') {
  }

...makes it clear that the string pointed to by *src, must contain a
terminating charater.

Does this sound about right??

Thanks Again!!

-Cary P

Quote:

> OK.  I'm stumped.

> This (not so) simple while loop copies one buffer to another.   This
> loop works, but what is the condition on which the WHILE loop exits??

> register char *src;
> char *dst;

> while (*dst++ = *src++) {
>   }

> Assume error checking has occurred, and that there is ample room in the
> "dst" buffer to to hold all of chars in the "src" buffer.

> --
> ///\\\///\///\\///\\\In the end, we will conserve only what we love and
> \\\ Cary Penniman ///respect.  We will love only what we understand. We
> /// Goleta, CA    \\\will only understand what we are taught or allowed
> \\\///\\\/\\\//\\\///to experience - Baba Dioum:African Conservationist

--
///\\\///\///\\///\\\In the end, we will conserve only what we love and
\\\ Cary Penniman ///respect.  We will love only what we understand. We
/// Goleta, CA    \\\will only understand what we are taught or allowed
\\\///\\\/\\\//\\\///to experience - Baba Dioum:African Conservationist


Sun, 03 Nov 2002 03:00:00 GMT  
 What is the conditional in this WHILE loop??

Quote:

>Thanks to all who answered!

>You confirmed my suspisions.  I think the final answer is...
>======================================
>register char *src;
>char *dst;
>char value;

>value = (*dst=*src);         /*  value evaluates to the value that is
>assigned to *dst */

>while (value != '\0') {       /* '\0' is the condition on which the while
>loop exits.  NOTE: value != 0 works as well */
>    dst++;
>    src++;                        /* increment pointers afterwards */
>    value = (*dst=*src);
>}

Not quite.  To make the above equivalent to
   while (*dst++ = *src++);
you need to add
   dst++, src++;
after the body of the loop.  The one-line version leaves both dst and src
pointing one past the terminating null character, while your version leaves
them pointing at that '\0'.
--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies


Sun, 03 Nov 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Optimising "conditional" while loop

2. Optimising "conditional" while loop

3. Using dummy loops to simplify conditional logic

4. Bug in for loop conditional?

5. Transformation of conditional expressions to conditional statements

6. I am new to programming and am lost

7. For loops into for loops

8. Loop or Loops in "C"

9. (Newbie) My Loop Isn't Looping - Aargh!

10. SEMA - WHILE loop in a FOR loop !

11. For loops into for loops

12. wanted - C and/or Fortran source for 24-loop Livermore Loops

 

 
Powered by phpBB® Forum Software