type | description |
---|---|
counting loop | Uses a loop control variable to count upwards or downwards (usually by an integer increment.) |
sentinel-controlled loop | Loop keeps going until a special value is encountered in the data. |
result-controlled loop | Loop keeps going until a test determines that the desired result has been achieved. |
Input from a file is usually done inside a loop that looks like this:
while ( ..... ) { String line = stdin.readLine(); ..... }
Other than the fact that there is an input statement in its body, this is an ordinary loop. Depending on the details of how it is implemented, it can be any of the three types of loops. Here is how the three types of loops are used for input loops:
type | description |
---|---|
counting loop | Increment a counter after each input line is read. |
sentinel-controlled loop | Read in lines until reaching a line that contains a special value. |
result-controlled loop | Read in lines until a desired result has been achieved. |
The most common input loops are the first two. Here is a typical file input problem:
Write a program to add up all the integers in a file except the first integer in the file which says how many integers follow.