Sequential Code (College Board AP® Computer Science Principles): Revision Note
Sequential execution & expressions
How does sequential code execute?
Sequential execution means that action statements in a program are carried out one at a time, in the order they appear, from top to bottom
Each statement must complete before the next one begins
The execution order directly determines what the program does and what output it produces
Changing the order of statements can change the program's behavior entirely
Ordered steps must reflect the logic of the task: inputs must be collected before they are processed, and processing must happen before output is displayed
x ← 5
x ← x + 1
DISPLAY(x)
This displays
6because the statements execute in order: assign 5, add 1, then display
Expressions
An expression is a combination of values, variables, and operators that is evaluated to produce a single value
Expression components include literals (e.g.
5), variables (e.g.score), and operators (e.g.+,*,=)When a program encounters an expression, it evaluates all components according to the evaluation order (rules of precedence) and reduces them to one result
result ← 10 + 3 * 2
This evaluates to
16because multiplication is performed before addition (standard mathematical precedence)
Code quality
What makes code high quality?
Code clarity means that the purpose of each section of code is easy to understand
Readability is improved by using meaningful variable names, consistent formatting, and logical organization
Following best practices makes code easier for others to read, debug, and maintain
Best practices for readable code
Keep each statement focused on a single action
Use comments to explain sections of code that are not immediately obvious
Organize code so that related statements are grouped together
Examiner Tips and Tricks
When tracing sequential code on the AP exam, work through each statement in order and update variable values as you go. Never skip ahead or assume the order does not matter. For expression evaluation, always apply mathematical precedence (multiplication and division before addition and subtraction) unless parentheses override the default order.
Code quality questions on the AP exam often ask you to identify which version of a program is most readable — look for meaningful variable names, appropriate use of comments, and logical organization as markers of high quality code
For the Create Performance Task, writing clear, readable code with meaningful variable names strengthens your written responses and makes your program easier for the reader to follow.
Worked Example
Consider the following code segment.
a ← 10
b ← a * 2
a ← 5
c ← a + b
DISPLAY(c)
What value is displayed when this code segment is executed?
(A) 15
(B) 25
(C) 30
(D) 35
[1]
Answer:
(B) 25 [1 mark]
bis assigned 20 (10 × 2) beforeais reassigned to 5, socevaluates to 5 + 20 = 25; the earlier reassignment ofadoes not affect the value already stored inb
Unlock more, it's free!
Was this revision note helpful?