Input, Process and Output with Sequential Structures: Programming Fundamentals (6)
Learn the sequential structure: how a program reads input, processes it and writes output, and how to turn a problem statement into a working program.
A sequential structure is a list of instructions that run one after another, from the first line to the last, exactly once each. Every program you have seen in this series so far is sequential. In this lesson you turn a problem statement into a complete program with three clear parts: it reads data, it processes that data, and it writes a result.
In this lesson you will:
- Read data from the user with
Readand show results withWrite. - Split a program into input, process and output.
- Follow a program by hand with a trace table.
- Build a complete program from a problem statement, one part at a time.
It takes about 30 minutes. You need a browser. Every example runs in the page.
Why order matters
A computer does not guess what you mean. It runs your instructions in the order you wrote them, top to bottom. If you calculate a result before you read the data, the result is wrong, or the program stops with an error. The sequential structure is the base of everything else. Conditionals and loops, which come in the next two lessons, only change which lines run and how many times. The lines inside them still run in order.
This matters more today, not less. When an AI agent hands you a program, your job is to read it and check it. The first question you ask is always the same one: in what order do these lines run, and what does each variable hold at each step? That is the skill this lesson trains.
The three parts of a program
In the pseudocode lesson you saw that most programs have three parts. Here they are again, with the instruction that does each job.
- Input. The program gets the data it needs. In pseudocode,
Read x;waits for the user to type a value and stores it in the variablex. - Process. The program does the work. These are assignments like
total <- a + b;and the math expressions you learned in operators and expressions. - Output. The program shows the result.
Write "Total: ", total;prints text and values on the screen.
A sequential program runs three blocks from top to bottom. Input: Read totalSeconds. Process: hours gets totalSeconds Div 3600, rest gets totalSeconds Mod 3600, minutes gets rest Div 60, seconds gets rest Mod 60. Output: Write shows the clock. Type a number and press Run to watch each line run in order.
1. Input
Read totalSeconds; totalSeconds = 3725 2. Process
hours <- totalSeconds Div 3600; hours = 1 rest <- totalSeconds Mod 3600; rest = 125 minutes <- rest Div 60; minutes = 2 seconds <- rest Mod 60; seconds = 5 3. Output
Write totalSeconds, " seconds is ", hours, ":", minutes, ":", seconds; 3725 seconds is 1:2:5 The output of one step is the input of the next one. The process needs the values that Read stored. The output needs the values that the process calculated. A sequential structure has one entry point at the top and one exit at the bottom.
Write and Read
Write takes one or more values separated by commas and prints them on one line. Text goes between double quotes. Variables and expressions print their value. Write adds no spaces between the values, so you put the spaces inside the quotes yourself.
Read takes the name of a variable that you declared before. The program stops, waits for the user to type a value and press Enter, and stores that value in the variable. The value must fit the variable’s data type. If the variable is an Integer and the user types abc, StepCode rejects the input and tells you why.
A good program always writes a message before each Read. Without it, the user sees a blinking cursor and has no idea what to type. This message is called a prompt.
Trace a program by hand
Before you write programs, learn to read them. A trace table follows a program line by line. It has one column per variable and one column for the output. Each row shows what changes after one line runs. The assignments of the course I taught at UCAB call this a “cold run” (corrido en frío in Spanish). You run the program in your head, not on the computer.
Here is a short program from one of the course assignments.
Fill the table before you press Run.
| Line | var1 | var2 | var3 | Output |
|---|---|---|---|---|
var1 <- 15; |
15 | |||
var2 <- 30; |
15 | 30 | ||
var3 <- var2 - var1; |
15 | 30 | 15 | |
first Write |
15 | 30 | 15 | var1: 15 var2: 30 |
second Write |
15 | 30 | 15 | var3: 15 |
This editor shows the Variables panel and the step buttons. Press the step button to run one line at a time and compare each step with your table.
You should see:
var1: 15 var2: 30
var3: 15
Build a program from a problem
Now you build a complete program. The problem comes from the first class of this unit:
Create a program that converts a number of seconds to hours, minutes and seconds (hh:mm:ss).
Before you write any code, answer three questions about the problem. This habit works for every problem in this course.
- What is the output? Hours, minutes and seconds.
- What is the input? One number: the total seconds.
- What is the process? One hour has 3600 seconds and one minute has 60 seconds. The hours are the whole times 3600 fits in the total. What is left over becomes minutes and seconds.
Two operators do the process. Div gives the whole part of a division, and Mod gives the remainder. For example, 3725 Div 3600 is 1 and 3725 Mod 3600 is 125. You met them in the last lesson, where they split a video of 3725 seconds into hours, minutes and seconds. Now you turn that idea into a complete program.
Div and Mod split the seconds into full blocks and a leftover. 3725 Div 3600 is 1 full hour, and 3725 Mod 3600 leaves 125 seconds. 125 Div 60 is 2 full minutes, and 125 Mod 60 leaves 5 seconds. Move the slider to try other numbers.
Full hours of 3600 seconds
totalSeconds Div 3600 = 1 and totalSeconds Mod 3600 = 125
The rest, in full minutes of 60 seconds
rest Div 60 = 2 and rest Mod 60 = 5
3725 seconds is 1:2:5
a full block (Div counts these) what is left over (Mod)
1. Read the input
Start with input only. Declare the variable, write a prompt and read the value. Then write it back, so you can check that the input works.
Press Run and type 3725.
You should see:
Enter the number of seconds:
You entered 3725 seconds
2. Calculate the hours
Add a variable for the hours and calculate it with Div. Keep the last line as a quick check.
Press Run and type 3725.
You should see:
Enter the number of seconds:
Hours: 1
3. Calculate minutes and seconds
The seconds that do not fit in a full hour are totalSeconds Mod 3600. Store them in rest. Then split rest the same way, with 60 instead of 3600.
Press Run and type 3725.
You should see:
Enter the number of seconds:
Hours: 1
Minutes: 2
Seconds: 5
4. Write the final output
Now give the program its final shape. Group the lines into input, process and output, and add a comment above each group. Comments start with // and the computer ignores them. Then write the result on one line, in the clock format.
Press Run and type 3725.
You should see:
Enter the number of seconds:
3725 seconds is 1:2:5
The program works, but a real clock shows 01:02:05. To add the zero, the program must decide: if the number is less than 10, write a 0 first. A sequential program cannot decide anything. Every line always runs. That is exactly the problem the next lesson solves.
In Python, the same program has the same three parts:
total_seconds = int(input("Enter the number of seconds: "))
hours = total_seconds // 3600
rest = total_seconds % 3600
minutes = rest // 60
seconds = rest % 60
print(total_seconds, "seconds is", f"{hours}:{minutes}:{seconds}")
Common mistakes
If you see "totalSeconds" has no value yet: give it one before using it., it means a line uses a variable before any line gave it a value. This usually happens when you put the process above the Read. Move the Read up, so it runs before the calculation.
If you see A Real cannot be stored where a Integer is expected: "Div" gives the whole division., it means you used / to divide two integers and tried to store the result in an Integer. The / operator always gives a Real, like 1.034. Use Div when you want the whole part.
If the output shows words stuck together, like You entered3725seconds, it means the spaces are missing. Write never adds spaces between values. Put them inside the quotes: "You entered ".
If the program waits and nothing happens, it is waiting for you at a Read. Type a value and press Enter. Then add a prompt before that Read, so the next user knows what to do.
Sequence is not the whole story
A sequential structure is the right tool when every input follows the same path. It stops working when the program must choose or repeat.
| You need to | Structure | Lesson |
|---|---|---|
| Run every line once, in order | Sequential | This one |
| Run some lines only when a condition is true | Conditional (If, Switch) |
Conditionals |
| Run some lines many times | Loop (While, For, Repeat) |
Loops |
Real programs mix all three. Even inside an If or a loop, the lines still run in sequence. So everything you learned here still applies there.
Practice exercises
These exercises come from the course practice guides. Try each one in an empty editor first. Then compare with the solution. Your messages can differ, but the numbers must match.
Packs of candies
A jar has some candies. Put them in packs of 5 first, then in packs of 2. Show how many packs of each size you need, and how many candies are left. Hint: use Div and Mod.
Input 28 gives:
Enter a number:
You need 5 packs of 5 candies.
You need 1 packs of 2 candies.
There are 1 candies left.
Area of a circle
Calculate the area of a circle from its radius. The area is pi times the radius squared. Define pi as a constant with the value 3.14. A constant is a value with a name that never changes while the program runs. You declared one in Variables and data types.
Input 5 gives:
Enter the radius:
The area is: 78.5
A small calculator
Ask for two integers. Show their sum, difference and product, with the numbers inside each sentence.
Inputs 8 and 3 give:
Enter the first number:
Enter the second number:
The sum of 8 and 3 is 11
The difference of 8 and 3 is 5
The product of 8 and 3 is 24
Rounded average
Ask for the grades of three exams and show the average, rounded to a whole number. Round is a function that StepCode already has.
Inputs 14, 16 and 17 give:
Grade of exam 1:
Grade of exam 2:
Grade of exam 3:
Your average is 16
Keep this program. In the next lesson you add a message that says if the student passed.
All the editors on this page run on StepCode, the pseudocode language I built for my students.
What you learned
- A sequential structure runs every instruction once, in the order you wrote it.
Readstores what the user types in a variable, andWriteprints text and values. Write a prompt before everyRead.- Most programs have three parts: input, process and output. Plan them before you write code.
- A trace table shows what each variable holds after each line. It is the fastest way to check a program, yours or an AI agent’s.
Next steps
A sequential program treats every input the same way. In the next lesson, conditionals, your programs start to make decisions with If, Else and Switch. You will finish the rounded average program and decide if a student passed.