Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
This page looks at for loops for input. To see a more detailed explanation using while loops, see C: While loops for input .
Input: Read the input data.
Process: Process the data, computing intermediate results along the way.
Output: Write the output results in a meaningful form.
2. For each loops
Many loops that process a set of items can be expressed by the following pseudo-code. A general form of pseudo-code for this problem is as follows.
FOR EACH item DO
process the item
END FOR
Note: The use of FOR EACH does not imply a for loop. In many cases, a while loop would be preferred.
3. Reading input
Consider the problem of reading nonnegative numbers from an input, one number per line, and printing the sum total of all of the numbers. So an input file input-01.dat containing the numbers
23
57
75
would result in the output of the number 155.
4. Math formula
Here is a mathematical formula for the problem. Given n values x0, x1, x2, ..., xn-1 the sum of the values is as follows.
Note: When writing a program, one must decide on the exact order in which the values will be processed.
5. Refined pseudo-code
The following pseudo-code solution results.
set the sum to zero
FOREACH data number xDO
add x to the sum END
output the sum
The name x has been used to indicate a data item. This name will eventually be represented as a variable in the program.
6. Loop types
There are several methods that are often used to determine the end of the data.
In a counter loop, the number of data items is known at compile-time.
In a header loop, the first data item represents the number of data items.
In a trailer loop, a special sentinel value guards the end of the data items.
In an end of file loop, the end of file marker is explicitly detected.
The following variable definitions apply.
dataPos1 represents the number of the current data item being processed.
dataLen1 represents the total number of data items.
dataValue1 represents the value of the current data item being processed.
dataSum1 represents the sum of the data items processed so far.
7. Counter loop
When the number of data items is known at compile-time, a counter loop can be used. For example, the three data items can be read with three input statements as follows.
Here is the C code.
8. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
9. Header loop
A header loop requires that the count of the number of items to be read precede the data, as in the following data set.
3
23
57
75
10. Header loop pseudo-code
If the number of data items precedes the data, a header loop can be used. A refined pseudo-code might be as follows.
get the header value n FOR EACH of n data values DO
get the next data value
process the data value
END FOR
Here is the C code.
11. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
12. Trailer loop
If the data ends with a special value, then a trailer loop can be used. This special value is called a sentinel, since the value guards, or marks, the end of the data.
A trailer loop requires that a special value, not used in the actual data, mark the end of the data, as in the following data values.
23
57
75
-1
A for loop does not work well for a trailer loop. Instead, use a while loop. Here is a program that uses a for loop for a trailer loop.
Here is the C code.
13. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
The pseudo-code for a trailer loop may be expanded as follows.
input a data item / trailer value into item WHILEitem is not the trailer value DO
process the item
input a data item / trailer value into item END
14. Endfile loop
An endfile loop explicitly detects the end of the input file, as in the following data file, input-04.
23
57
75
The pseudo-code for an endfile loop is as follows.
WHILE not eof DO
process item
END
A for loop does not work for an endfile loop. Instead, use a while loop.