AP Computer Science Principles · Unit 1 lecture · Part 4 of 11 · 7 min

1.2 Inputs, outputs and eventsWhat goes in, what comes out, and what sets it off

Mr. Thompkins
Think about the last app you used. You tapped, typed, maybe talked to it, and it answered with sounds, pictures and buzzes. Every one of those is an input or an output, and the exam expects you to sort them quickly.

Program input is data sent to a computer for processing by a program. Input comes in four forms: tactile (a tap, a click, a button press), audio (speech into a microphone), visual (a camera image, a scanned code), and text (typed words or numbers). Input can come from a user or from another program, like a weather service sending today's forecast to your phone, or a file the program reads.

Program output is any data sent from a program to a device: text or images on a screen, sound from a speaker, a vibration, a message sent to another program. Output is usually based on the program's input, or on its prior state, meaning values it already had stored. A step counter shows a number based on what its sensor reported (input) and how many steps it had counted so far today (prior state).

?Wait, what does INPUT() do on the exam?
INPUT() is on the AP Exam Reference Sheet. It accepts a value from the user and returns it, so a line like age ← INPUT() waits for the user to type something and stores it in age. DISPLAY(expression) is its partner: it shows a value on the screen, followed by a space.

Events and event-driven programs

Many inputs arrive as events. An event is associated with an action and supplies input data to a program. Events are generated when a key is pressed, a mouse is clicked, a program starts, a timer runs out, or any other defined action happens that affects the flow of execution.

That leads to a big idea about how programs run. Some programs run sequentially: statement 1, then statement 2, then 3, top to bottom, like the tip splitter. In event-driven programming, statements run when they are triggered by an event, not just in the order they were written. A game's jump code sits there doing nothing until the space bar is pressed. The "Send" code in a chat app waits for the tap. Most apps you use are event-driven.

EventKind of inputCode that runs
Tap the Add buttonTactileSave the new task to the list
Say "next song"AudioSkip to the next track
Camera sees a QR codeVisualOpen the linked menu
Type a city and press EnterTextLook up its weather
App is openedA defined actionLoad the saved settings
Events, the kind of input each one is, and the code each one triggers.

Sequential flow

  • Statements run in order, top to bottom
  • The program decides when each line runs
  • Example: a script that computes a grade and displays it

Event-driven

  • Code runs when an event triggers it
  • The user (or a sensor, or a timer) decides when
  • Example: a music app that waits for you to tap Play
?Wait, what's "prior state"?
Prior state is what the program already has stored when new input arrives: a score, a list of saved tasks, the current level. Two people pressing the same button can see different outputs because their programs are in different states.
Pause and think
A smart doorbell records video when it detects motion and sends a notification to your phone. Name one input, one output, and the event.
I thought about it. Show me
Input: the video from its camera (visual) or the motion sensor reading. Output: the notification sent to your phone, and the saved video. Event: motion being detected, which triggers the recording code.
Sort the inputs and outputs
A language app shows a picture of a dog, plays the word "perro" out loud, and asks the user to say it back. If the microphone hears the right word, the phone buzzes once and the score goes up by 10. If not, the app shows the spelling. List every input and output, with its kind, and name an event.
  1. Inputs: the user's spoken word (audio). Also any taps to start or skip (tactile).
    Why?
    Inputs are data sent into the program for processing.
  2. Outputs: the picture (visual), the spoken word "perro" (audio), the buzz (tactile), the new score and the spelling on screen (visual or text).
    Why?
    Outputs are data the program sends to a device: screen, speaker, vibration motor.
  3. Event: the microphone finishing a recording, or a tap, triggers the code that checks the answer.
    Why?
    An event is an action that supplies input and triggers code.
  4. Notice the score: its output depends on the input (right or wrong) and on prior state (the score before).

Answer: Inputs: spoken word (audio), taps (tactile). Outputs: picture (visual), spoken word (audio), buzz (tactile), score and spelling (visual text). Event: the recording finishing, or a tap, triggers the checking code.

Stuck? Get a hint
  1. Ask for each item: is it going into the program, or coming out of it?
  2. Sounds can be both: the app plays one (output) and hears one (input).
  3. An event is the action that makes code run.
Check yourself
Which of the following is the best example of event-driven programming?
  1. AA program that computes the average of 100 numbers and then displays it
  2. BA drawing app that draws a dot wherever the user clicks
  3. CA program that displays the numbers 1 to 10 in order
  4. DA calculator that always multiplies the same two numbers when it starts
Show the answer

B. Each click is an event, and the dot-drawing code runs only when a click triggers it. The other three run straight through in a fixed order, which is sequential flow.

Check yourself
A fitness watch vibrates when the wearer has been sitting for an hour. The vibration is
  1. Aa tactile input
  2. Ba tactile output
  3. Can audio input
  4. Dan event that the user generates
Show the answer

B. The vibration is data the program sends to a device, the vibration motor, so it is an output, and you feel it, so it is tactile. The trap is "tactile input": a tap on the watch would be a tactile input.

  • Input: data sent to a program. Tactile, audio, visual or text, from a user or another program.
  • Output: data sent from a program to a device. Usually based on input or prior state.
  • An event is an action (click, key press, program start, timer) that supplies input and triggers code.
  • Event-driven programs run code when events trigger it, not only in order from top to bottom.