jbm@jbm.nada.kth.se (Jonas M�ls�) writes: > Also, I have never encountered a real situation > where I have needed an extra flag in the predicate. > One can actually do fine without them. As an example, > I hereby present to you three variations of str_index. > > The first one strictly exits at the top. The other two > in the middle. The first one is much easier to reason > about, and to understan, IMHO. Anyway, introducing an > extra flag in the predicate is very often a sign of > "patching the loop". Done right, one does not need it. > > > > /** str_index > * > * str_index returns a pointer to the first occurrence of > * the given character in the given string. > * If the given character does not occur in the given > * string, str_index returns NULL. > * > * If the given pointer is NULL, the result is undefined. > * > */ > > Here's another idea: char * str_index ( char *s, /* string to search */ char c) /* character to search for */ { for (;;) if (*s == c) return s; else if (*s == '\0') return NULL; else s++; } I don't know if this is typical C code, however. I suspect one would move some of the work up into the for loop declaration: for (; *s != '\0'; s++) if (*s == c) return s; return NULL; An Ada programmer has an easier go of it: function Get_Position (S : String; C : Character) return Natural is begin for Index in S'Range loop if S(Index) = C then return Index; end if; end loop; return 0; end Get_Position;