Almost every programming language handles program input/output (I/O) differently. However, what is common to most programming languages are the two types of I/O: user I/O and file I/O.

User I/O

User I/O is interactive since this type of input is generally accomplished via the keyboard or mouse and a monitor. A typical scenario for user I/O is a prompt on the computer monitor asking a user for some needed information. Once this information is entered, the program assigns the input to a variable and continues its execution. A very common prompt for user input and output is called a command prompt. The diagram below shows a command prompt on a Windows NT computer:

Notice how this I/O is interactive. The command prompt displays the string "Enter a number between 1 - 10: " on the monitor, and the user responds by typing a number on the keyboard. Since there is no standard way to represent interactive I/O in programming languages, we simply use the statement InputLine(<data>) to represent user input and OutputLine(<data>) to represent user output. Using these two statements, we can write code to represent the diagram above.

Integer myInput

Call OutputLine("Enter a number between 1 - 10: ")
Call InputLine(myInput)
   .
   .  
<do something with the input>
   .

Perhaps you noticed that InputLine() and OutputLine() both look similar to subprograms. In fact, many programming languages use special procedures to implement user I/O. This is a logical choice since I/O typically requires us to specify some variables to hold the data that is transferred during the I/O operation. We can use the subprogram parameters to accomplish this. Notice that InputLine() always takes a variable as its parameter and the data input by the user is assigned to this variable. In contrast, OutputLine() takes a variable or a string (i.e. a line of text) as its parameter and displays this parameter to the user.

Now that you are familiar with the concept of user I/O, take a look at the following code segment. Trace the code segment and then draw a picture of what you think will happen when this code is executed and the user inputs the number 4.

Integer myInput
Integer i := 0

Call OutputLine("Enter a number please: ")
Call InputLine(myInput)

While (i < myInput)
   OutputLine(i)
   OutputLine("says hello!")
   i := i + 1
EndWhile

view the answer

Click on the link below the code box to view the answer. Is your diagram similar to the command prompt displayed in the new window? Perhaps you were a little surprised that each item output was on a different line. This is because the OutputLine and InputLine procedures always operate on a single line. Each programming language has its own collection of I/O procedures that allow users to control exactly how input is collected and output is displayed rather than simply using a new line each time. However, we will not cover these details in our study.

File I/O

Often there are times when we need to give input to a program that does not need to be interactive. For example, suppose we had a program that calculated the grades for a class with 200 students. If the program prompted us to enter the grade for each student, we would quickly grow bored and give up. Instead what we would like to do is collect all the information in one place and allow the program to read what it needs from that location rather than asking us for the data. The solution to our problem is file I/O.

File I/O allows us to store data in a file on the computer and then read the data from this file. This type of input is not interactive since the computer does not wait for a response from the user. Instead, the data is read directly from a file specified in the program. Notice how this is different from user I/O. With user I/O, the program halts each time it reaches an input statement. Execution cannot continue until the program receives a response from an input device like the keyboard. However, file I/O allows the computer to get the input for itself without the help of the user so the program does not need to halt at each input. This makes file I/O ideal for processing large amounts of data at once.

As with user I/O, file I/O typically done using a collection of procedures that are defined by a given programming language. Of course, these procedures will need an extra parameter that tells them which file to use for input or output. Since these details are also very languages specific, we will not cover them in this lesson.

Sample program with user I/O

To give you a feel for how user I/O works, the program below shows a simple call to a function named FindSmallest. This function has three input parameters: an array of numbers, a low index, and a high index. The two indices define a subrange of the array on which the function will perform its operation. So the following call to FindSmallest will operate on the array elements between 3 and 10:

FindSmallest(array[], 3, 10)

While you run the program, trace through the code and see if you can figure out what the FindSmallest function is doing.

Integer number
Integer numbers[]
Integer arraySize
Integer smallest
Integer count := 1

Call OutputLine("Select a size between 1 and 10:")
Call InputLine( arraySize )

While ( count <= arraySize )
   Call OutputLine( count )
   Call OutputLine("Input a number:")
   Call InputLine( number )
   numbers[count] := number
   count := count + 1
EndWhile

smallest := Call FindSmallest(numbers[], 1, arraySize)
Call OutputLine("The smallest number was:")
Call OutputLine(numbers[smallest])

run the code

If you have not already discovered it, the FindSmallest function searches the numbers[] array for the smallest number. However, notice that the function does not actually return the smallest number but the location of the smallest number. The final output statement in the program uses this location to display the smallest number. The function also operates on a specific range of the numbers[] array. In this case, the range was always the entire array, but we could use FindSmallest to search for the smallest number in a specific part of an array. We will see an example of this in the next lesson.