Yes.
Here is a C program that implements the program design:
#include <stdio.h>
#include <ctype.h>
void main( void )
{
int ch;
ch = getchar(); /* attempt to read the first character */
while ( ch != EOF ) /* while not end-of-file */
{
if ( 'a' <= ch && ch < 'z' )
{
ch -= 'a' - 'A' ; /* convert to upper case */
}
putchar( ch );
ch = getchar();
}
}
It is useful to recall that an assignment statement is an expression that has a value. The value of the entire expression is the value that was placed in the variable. So the value of
ch = getchar();
is whatever value was put into ch, including EOF when that happens.
So the above code can be more conveniently written as:
#include <stdio.h>
#include <ctype.h>
void main( void )
{
int ch;
while ( (ch = getchar()) != EOF )
{
if ( 'a' <= ch && ch < 'z' )
{
ch -= 'a' - 'A' ; /* convert to upper case */
}
putchar( ch );
}
}
This is still structured code, although now some of the stucture is embedded in the loop condition.
Why are there parentheses around (ch = getchar()) ?