6.3 - Inputs & Outputs
What inputs and outputs are in programming
In programming, inputs and outputs form the main ways that a program interacts with the world outside it. They allow data to flow into and out of the program, making it possible for users to provide information and receive results.
Inputs are pieces of data that a computer program receives from external sources. This data can then be used within the program to make decisions or perform calculations. Outputs are the results or information that the program sends back to the user or another system. Without inputs and outputs, programs would be isolated and unable to respond to real-world needs.
Different forms of inputs
Inputs can arrive in various forms, depending on the program's purpose and the devices involved. This variety allows programs to handle real-world data effectively.
Common forms of inputs:
- User text - Typed information from a keyboard, such as names or numbers.
- Sensor signals - Data from devices like temperature sensors or motion detectors, which send automatic readings.
- Other sources - Inputs could also come from files, networks, or even other programs.
Example of a text input in pseudocode
Programs often prompt users for input and store it in a variable (a named storage location for data). Here's how this might look:
In this example:
- The text "Enter your age" is displayed as a prompt to guide the user.
- The program waits for the user to type a response and press return.
- The user's input is then assigned to the variable
agefor use in the program.
This approach makes programs interactive, as the data provided can change how the program behaves.
Different forms of outputs
Outputs allow programs to communicate results back to the user or external systems. Like inputs, outputs can take many forms to suit different needs.
Common forms of outputs:
- Text on screen - Simple messages or results displayed for the user to read.
- Images or graphics - Visual elements, such as pictures or charts.
- Sounds - Audio feedback, like beeps or spoken words.
- Other forms - Outputs could also include writing to files, sending data over networks, or controlling devices like lights.
Example of a text output in pseudocode
A basic way to output information is by printing text to the screen. This is done using a command that displays the specified content.
In this example:
- The
PRINTfunction is the command that handles the output. - The text "Hello user!" is what appears on the screen.
- This creates a direct way for the program to share information with the user.
Outputs like this help users see the results of their inputs or the program's calculations.
Working with strings for text data
When dealing with text in programs, we use something called a string. A string is a sequence of characters that represents text, which can include letters, numbers, symbols, or spaces.
Strings are enclosed in quotation marks to tell the program that the content is text, not a command or number. Double quotation marks (") are commonly used, though single quotes (') sometimes appear too.
Example of creating and outputting a string in pseudocode
This code:
- Assigns the string "Coding is a great skill!" to a variable called
message. - Uses
PRINTto display the string on the screen. - The output would be: Coding is a great skill!
Strings are fundamental because much of the data we handle in everyday programs, like names or messages, is text-based.
Joining strings together using concatenation
Sometimes, you need to combine multiple strings to create a new one. This process is called concatenation, which means linking things together.
Concatenation is often done using the + operator, which joins the strings end to end. However, it's important to include any necessary spaces manually, as the program won't add them automatically.
Example of string concatenation in pseudocode
In this example:
string1andstring2hold separate pieces of text.- The + operator combines them, with " " adding a space in between.
- The output would be: The capital of France is Paris
Why spaces matter in concatenation
Strings store exactly the characters you provide, nothing more. If you concatenate without adding a space, like "hello" + "world", the result is "helloworld" with no gap. Always include spaces where needed to make the output readable, such as by adding " " in the concatenation.
This technique is useful for building dynamic messages, like combining a user's name with a greeting.