Article
Your first program
Your first program is only one line long. It still teaches you the two most important ideas in programming: a computer follows each instruction exactly, and it follows them in order.
One statement: DISPLAY
The exam shows code in AP pseudocode, a notation made for the AP exam. You cannot run it. In this course you also write the same program in Python, which you can run. Every example has both, so you can read the exam's notation and try the idea yourself.
The statement that shows something on the screen is DISPLAY in AP pseudocode and print in Python:
DISPLAY("Hello!")Press Run. The program shows Hello! without the quote marks. The text inside the quote marks is called a string. The computer shows a string exactly as you typed it. (A later lesson is all about strings.)
Now change the text between the quotes and run it again. The computer always shows what you wrote, because it does exactly what the statement says.
Check your understanding
What does this program show on the screen?
DISPLAY("Hi there")Many statements, one after the other
A program can have many statements. The computer follows them in order, from the top to the bottom, one at a time. This is called sequencing.
DISPLAY("Ready")
DISPLAY("Set")
DISPLAY("Go")The program shows Ready, then Set, then Go. If you swap the first and last lines, the program shows Go, then Set, then Ready. The order of the statements matters. The computer never fixes your order for you.
One small difference: on the exam, DISPLAY shows its value and then a space, so several DISPLAY statements appear on one line. The exam would show the result above as Ready Set Go. Python's print starts a new line each time instead. The meaning is the same.
Showing the result of a calculation
DISPLAY can show a number, or the answer to a sum. The computer works out the sum first and then shows the answer:
DISPLAY(2 + 3)
DISPLAY("2 + 3")The first statement shows 5. The second shows 2 + 3, because anything inside quote marks is just text. No quote marks means the computer works it out. Quote marks mean it copies the characters.
Reading a program with a trace table
To be sure what a program does, follow it as the computer does. Write one row for each statement and note what it shows. This is a trace table. Here is the trace for a four-statement program:
DISPLAY("Score:")
DISPLAY(10)
DISPLAY(5 * 2)
DISPLAY("Done")| Statement run | What it shows | Screen so far |
|---|---|---|
DISPLAY("Score:") | Score: | Score: |
DISPLAY(10) | 10 | Score: 10 |
DISPLAY(5 * 2) | 10 (5 times 2) | Score: 10 10 |
DISPLAY("Done") | Done | Score: 10 10 Done |
Notice that DISPLAY(10) and DISPLAY(5 * 2) show the same thing: 10.
Check your understanding
What is displayed when this program runs? (Each DISPLAY shows its value followed by a space.)
DISPLAY(4)
DISPLAY(2 * 3)
DISPLAY("5")Worked example: put the lines in the right order
A student wants a program that shows 1, then 2, then 3, then Go. Her code is:
DISPLAY(2)
DISPLAY("Go")
DISPLAY(1)
DISPLAY(3)Trace it: it shows 2 Go 1 3. This is not what she wanted. The statements are all correct, but they are in the wrong order. She does not need new statements. She only needs to move them so the order matches the output she wants:
DISPLAY(1)
DISPLAY(2)
DISPLAY(3)
DISPLAY("Go")A mistake in a program is a bug. Putting correct statements in the wrong order is one of the most common bugs.
Common mistakes
- Leaving out the quote marks around text.
print(Hello)makes Python look for something calledHello, and it stops with an error. Text needs quote marks. - Putting quote marks around a calculation.
DISPLAY("2 + 3")shows2 + 3, not5. - Spelling a statement wrong. Python is picky:
Print("Hi")with a capital P does not work. The rules of spelling and punctuation in a language are its syntax. A program with a syntax error does not run at all. - Thinking the computer will fix the order. It will not. It runs the statements in the order you wrote them.
- Forgetting the brackets.
DISPLAYandprintboth need their value inside brackets.
On the exam
You will see a short code segment and be asked what it displays. Go from the top to the bottom, write down what each DISPLAY shows, and join the results with spaces. Check quote marks carefully: text shows as written, and anything else is worked out first. Some questions ask you to reorder or choose lines so that the program shows a given result.
Key words
DISPLAY: the AP pseudocode statement that shows a value (Python: print).
String: text inside quote marks.
Sequencing: running statements in order, from the top to the bottom.
Trace table: a table that follows a program one statement at a time.
Bug: a mistake in a program.
Syntax: the exact spelling and punctuation rules of a language.