Welcome to Big Idea 3: Algorithms and Programming!

Hi there! Welcome to what many people call the "heart" of computer science. In this section, we are going to learn how to give instructions to a computer so it can solve problems for us. Whether you are building the next big social media app or a simple calculator, you use Algorithms (the plan) and Programming (the action) to make it happen. Don't worry if you've never coded before—we're going to break this down step-by-step!

1. Variables and Assignments

Think of a variable as a small storage box with a name on it. Inside the box, you can keep one piece of information. When you want to change what's inside, you "assign" a new value to it.

Key Concepts:

  • Assignment: In the AP exam, you will see an arrow \( \leftarrow \) used to give a value to a variable. For example, score \( \leftarrow \) 10 means the variable "score" now holds the number 10.
  • Data Types: Computers handle different kinds of data. The main ones are Numbers (like 5 or 3.14), Booleans (True or False), and Strings (text wrapped in "quotes").

Common Mistake: Remember that when you update a variable, the old value is gone forever! If x was 5 and you say x \( \leftarrow \) 10, the 5 is deleted.

2. Math and Logic

Computers are basically super-fast calculators. They use standard math like addition (+), subtraction (-), multiplication (*), and division (/).

The Modulo Operator (%)

This is one that trips students up, but it's simple! Modulo gives you the remainder of a division problem.
Example: 13 / 5 is 2 with a remainder of 3. So, 13 MOD 5 = 3.

Quick Tip: Modulo is great for checking if a number is even or odd. If number MOD 2 is 0, the number is even!

Comparison and Boolean Logic

We use logic to help the computer make decisions. This usually results in a Boolean value: True or False.

  • Relational Operators: \( = \), \( \neq \), \( > \), \( < \), \( \geq \), \( \leq \).
  • Logical Operators: NOT (flips the value), AND (both must be true), OR (at least one must be true).

3. Conditionals (Selection)

A conditional statement tells the computer to run certain code only if a specific condition is met. We call this Selection.

The IF Statement: If it is raining, take an umbrella.
The IF-ELSE Statement: If you have money, buy a snack; otherwise (else), go home.

Key Takeaway: Conditionals allow our programs to be flexible and react to different situations!

4. Iteration (Loops)

Computers are great at doing the same thing over and over without getting bored. This is called Iteration or looping.

  • Repeat n times: You know exactly how many times to do something.
  • Repeat Until: You keep going until a certain condition becomes true (e.g., Repeat until the battery is 100%).
  • For Each: Used to look at every single item in a list.

5. Lists (Data Collections)

A list is like a digital shopping list. It allows you to store multiple items under one variable name. Instead of having variables item1, item2, item3, you just have one list called inventory.

CRITICAL RULE: In the AP CSP pseudo-code, list indices start at 1. In many "real" languages like Python, they start at 0. Always double-check this on the exam!

List Operations:

  • INSERT: Adds an item at a specific spot.
  • APPEND: Adds an item to the very end.
  • REMOVE: Deletes an item.
  • LENGTH: Tells you how many items are in the list.

6. Procedures and Libraries

A procedure (also called a function) is a named group of programming instructions that perform a specific task. Think of it like a "recipe" that you can call by name whenever you need it.

Parameters: These are "input" values you give to a procedure so it can do its job. For example, a procedure calculateArea(width, height) needs the width and height to work.

Libraries: These are collections of procedures that have already been written by someone else. You can use them so you don't have to "reinvent the wheel."

7. Developing Algorithms

An algorithm is just a step-by-step process for solving a problem. Every algorithm is made of three building blocks:

  1. Sequencing: Doing steps in order (Step 1, then Step 2, then Step 3).
  2. Selection: Making decisions (IF statements).
  3. Iteration: Repeating steps (Loops).

Searching Algorithms

How do we find a specific item in a big list?

  • Linear Search: Checking every item one by one from start to finish. It works on any list, but it's slow for big lists.
  • Binary Search: Much faster, but the list MUST be sorted first. It works by starting in the middle and throwing away half the list each time.

Analogy: Finding a word in a dictionary. You don't start at page 1 and read every word (Linear). You jump to the middle and narrow it down (Binary).

8. Efficiency and Decidability

Not all algorithms are created equal! Some are faster than others.

  • Efficiency: We measure this by how many times a step is repeated as the input size grows.
  • Heuristics: Sometimes a problem is too hard for a computer to find the perfect answer in a reasonable time. A heuristic is a "good enough" shortcut or rule of thumb.
  • Undecidable Problems: Believe it or not, there are some problems a computer cannot solve. The most famous is the Halting Problem (determining if a program will eventually stop or run forever).

Quick Review: The Essentials

Variable: A named container for data.
Selection: Using "If" to choose a path.
Iteration: Using loops to repeat code.
List Indexing: Starts at 1 for the AP exam!
Binary Search: Fast, but needs a sorted list.
Don't worry if this seems tricky at first! Programming is like a muscle—the more you practice thinking through the steps, the stronger it gets. You've got this!