`if (a = b)' (was Standard indentation?)

Dave Jones djones at megatest.UUCP
Tue Dec 13 10:54:07 AEST 1988


There is a construct that is a little awkward using standard
(Pascal-like) structured statements:  You want to read a value
from an external source, exit the loop if the value is a sentinal,
or process the value and continue if not a sentinal.

{
  int ch;
  do
    { 
      ch = getchar();
      if(ch != EOF) process(ch);
    } 
  while (ch != EOF);
}


The expression !EOF gets evaluated twice.  A compiler that
does common subexpression-removal and global flow-analysis 
would fix things, but not all compilers are that smart.  
Besides, I'm one of those old fashioned guys who wants the
compiler to do what I say. Or rather, I would like to be able 
to say what I really want the compiler to do.

So we could do this:

for(;;)
  {
     int ch = getchar();
     if(ch == EOF) break;
     process(ch);
  }

This is nice, in as much as it moves the declaration of ch one
level deeper.  But it has the somewhat unstructured "break".
Still, not too bad.  I prefer the following:


{ int ch;
  while( (ch = getchar()) != EOF )
    process(ch);
}


This says it almost literally,  "While I get a ch that is not 
a sentinal, I want to continue processing."



More information about the Comp.lang.c mailing list