Welcome to Big Idea 3: Programming Fundamentals!
In this chapter, we are diving into the most basic building block of any computer program: Variables and Assignments. Don't worry if programming sounds intimidating at first! Think of a program like a recipe. To cook the meal, you need containers to hold your ingredients. In computer science, variables are those containers.
By the end of these notes, you will understand how to name, store, and change data using variables. This is the first step toward building your own apps and games!
1. What is a Variable?
A variable is an abstraction inside a program that can hold a value. Each variable has a name that you (the programmer) choose. This name helps you refer to the data without having to remember the exact value every time.
Analogy: The Labelled Box
Imagine you have a cardboard box. You write "Score" on the outside of the box with a marker. Inside the box, you place a piece of paper that says "10".
- The variable name is "Score".
- The value inside is 10.
- If the player gets a point, you throw away the "10" and put in an "11". The box name stays the same, but the data inside changes.
Key Terms to Know:
Value: The actual data stored in the variable (like a number or a word).
Data Type: The kind of data being stored. While the AP CSP Exam is "language-independent," you will usually see numbers, Booleans (True/False), and Strings (text).
2. The Assignment Operator
To put a value into a variable, we use an assignment operator. On the AP CSP Exam, this is represented by a left-pointing arrow \( \leftarrow \).
The Syntax:
\( a \leftarrow \text{expression} \)
This statement tells the computer to evaluate the expression on the right first, and then store the result in the variable on the left.
How to read it:
If you see \( \text{score} \leftarrow 5 \), read it as: "The variable 'score' gets the value 5."
Important Rule: The Right-to-Left Rule
The computer always solves the right side before it updates the variable. For example:
\( \text{num} \leftarrow 10 \)
\( \text{num} \leftarrow \text{num} + 5 \)
In the second line, the computer looks at the right side first: \( 10 + 5 = 15 \). Then it puts that \( 15 \) into the box named \( \text{num} \). The old value (\( 10 \)) is gone forever!
Quick Review: Does the order matter? Yes! The variable receiving the value must always be on the left side of the arrow.
3. Generalizing Data Sources
One of the most powerful things about variables is that they allow us to generalize data. This is a form of abstraction (Practice 3.A).
Imagine you are writing a program to calculate a 5% tax on a purchase. You could write:
\( 10.00 \times 0.05 \)
But that only works for a \$10 purchase. Instead, we use a variable:
\( \text{totalCost} \leftarrow \text{purchaseAmount} \times 0.05 \)
By using the variable \( \text{purchaseAmount} \), your code can now handle any number the user provides. We have "generalized" the solution to work for many different inputs rather than just one specific case.
4. Input and Output
Variables often get their values from the user or show their values back to the user. The AP CSP Reference Sheet uses these two commands:
1. INPUT(): This accepts a value from the user. It is usually used with an assignment.
Example: \( \text{userName} \leftarrow \text{INPUT()} \)
This waits for the user to type something and saves it in the variable \( \text{userName} \).
2. DISPLAY(expression): This shows the value on the screen followed by a space.
Example: \( \text{DISPLAY("Hello")} \)
Example: \( \text{DISPLAY(score)} \)
5. Common Mistakes to Avoid
Don't worry if this seems tricky at first! Here are a few things that trip up many students:
- Confusing Variable Names with Values: \( \text{DISPLAY(x)} \) shows the value inside the variable \( x \). \( \text{DISPLAY("x")} \) shows the actual letter "x" as a string.
- One Value at a Time: A variable can only hold one thing at a time. When you assign a new value, the old one is "overwritten" or deleted. (Note: To store multiple values at once, we use Lists, which you will learn about in chapter 3.2).
- Undefined Variables: You cannot use a variable in an expression (like \( a + 5 \)) if you haven't assigned it a value first!
Chapter Summary (Key Takeaways)
1. Variables are containers: They store values that can change during a program.
2. Assignment is one-way: The operator \( \leftarrow \) moves data from the right-side expression into the left-side variable.
3. Generalization: Using variables allows a program to work with many different data sources rather than just fixed numbers.
4. Notation: Get comfortable with the \( a \leftarrow \text{expression} \), \( \text{INPUT()} \), and \( \text{DISPLAY()} \) commands used on the AP Exam.
Next Steps: Now that you know how to store values, check out the chapter on Mathematical Expressions to see how we can calculate new values using math operators like \( + \), \( - \), and \( \text{MOD} \)!