We first encountered looping or iteration or repetition in in the pounds.c program. In that example we printed a dollar amount and the equivalent british currency amount for a number of different dollar amounts; we repeated a process for each dollar amount.
Here we will explore the while statement of C and show how it can be used in a variety of contexts, with numbers and characters, to repeat a process a number of times.
The while statement consists of the while keyword followed by condition enclosed between ( and ) which is followed by a statement or block of statements enclosed between { and }. For example:
main() { int n ; n = 0 ; while ( n < 10 ) { printf( "%d\n" ) ; n = n + 1 ; } }
would print the numbers 0 through 9, one per line.
Note that we make use of the return value of the scanf() function. The value returned by scanf is the number of values read which match the format specifier (in our case, "%f"). If There is no number read, we terminate our loop.
We also make use of the increment operator ("++") in the statement:
You may think of it as being equivalent to:count ++ ;
count = count + 1 ;
Without further delay, here is our number-counting program.
/* number_count.c program to count numbers entered. */ #includemain() { float f ; int count ; count = 0 ; while ( scanf("%f" , &f ) == 1 ) count ++ ; printf( "%d numbers\n" , count ) ; }
Note that this program makes use of the getchar() function, which reads a single character of input and returns that character as the value of the function. If there is an error reading the character, or if the end of input is reached, getchar() returns a special value, represented below by "EOF".
We also make use of the return value of the assignment statement. In C, an assignment statement has as its value, the value assigned to the variable to the left of the "=". In our example, below, the while continues to execute as long as we assign values to the variable "c" which are not equal to the special value EOF.
/* char_count.c A program to count characters of input. */ #includemain() { int c ; int count ; while ( ( c = getchar() ) != EOF ) count ++ ; printf( "%d characters\n" , count ) ; }
Note that when we wish to refer to a character as a literal (by its "printed name"), we enclose it in single quotes ('). In C, when we wish to refer to a single character, we enclose it in single quotes; when we wish to refer to a string of characters, we enclose them in double quotes. Further note that we can use the escape character ("\") to refer to certain non-printing characters, such as the end of line character ('\n').
/* letter_count.c A program to count letters in input. */ #includemain() { int c ; int count ; while ( ( c = getchar() ) != EOF ) if ( ( c >= 'A' ) && ( c <= 'Z' ) || ( c >= 'a' ) && ( c <= 'z' ) ) count ++ ; printf( "%d letters\n" , count ) ; }
Here, the "<" tells the system to read input for the program from the file "alice.txt" (instead of waiting for you to enter it followed by CTRL-D).$ ./letter_count < alice.txt 133 letters
Copyright © 1999, Ray Ontko (rayo@ontko.com).