Topic 1.1 — Introduction to Algorithms, Programming, and Compilers

Goal: define algorithms and programs, explain how source code is compiled and executed, and recognize common error categories (syntax, run-time, logic).

The big idea

In AP CSA, you solve problems by writing a clear algorithm (a step-by-step plan), then translating it into a program written in Java.

Computers follow instructions exactly—so clarity and order matter.

Key vocabulary

  • Algorithm: a finite, ordered set of steps that solves a problem.
  • Program: an algorithm written in a programming language (Java) that a computer can execute.
  • Compiler: checks code for syntax errors and translates it into a form that can run.
  • Run: executing the program’s statements in order.
  • IDE: software that helps you write, compile, run, and debug programs.

Algorithm first (language-independent)

You can describe an algorithm in words, a diagram, or pseudocode. The important part is that the steps are clear and in order.

Example algorithm (in words)

  1. Start with a total of 0.
  2. Add 5 to the total.
  3. Double the total.
  4. Store the result.

This describes what to do, not the exact Java syntax.

Same idea, as a Java program

A program expresses the algorithm in exact Java statements.

public class TotalDemo {
  public static void main(String[] args) {
    int total = 0;
    total = total + 5;
    total = total * 2;
    // result is stored in total
  }
}

(Notice: no output here—printing is covered in Topic 1.3.)

Compile vs. run (the workflow)

  • Write Java source code (.java).
  • Compile: the compiler checks syntax and translates your code.
  • Run: the program executes statements in order.
  • Test: compare behavior to what you intended.
  • Debug: fix issues and try again.

Key rule: if it doesn’t compile, it can’t run.

What an IDE helps with

  • Editing code (auto-indent, highlighting, autocomplete).
  • Compiling and running quickly.
  • Showing error messages and where to look.
  • (Later) debugging step-by-step.

Three error types you must recognize

A big part of early CSA is learning to read errors and classify what kind they are.

1) Syntax error (compile-time)

Java grammar is broken → compiler stops → program won’t run.

// Missing semicolon (won't compile)
int x = 5

2) Run-time error (exception)

Compiles, starts running, then crashes while executing.

int a = 10;
int b = 0;
int c = a / b;   // ArithmeticException at run time

3) Logic error (wrong result)

Runs without crashing, but the result doesn’t match the goal.

// Intended: average of 6 and 8
int avg = 6 + 8 / 2;    // wrong grouping for the goal
// Correct grouping would be: (6 + 8) / 2

Debug checklist (starter)

  • Read the message (compiler/exception text matters).
  • Locate the line and inspect nearby lines too.
  • Trace the steps in order (what happens first? next?).
  • Change one thing at a time and re-test.

Sequencing: order matters

Programs execute statements in the order they appear (top → bottom), unless control structures change the flow (later units).

int x = 3;
x = x + 2;   // x becomes 5
x = x * 4;   // x becomes 20

If you change the order of these steps, the final value can change.

Mini reasoning check

Which algorithm is clearer?

// A: vague
// "Do the math and get the answer"

// B: clear
// 1) Start with total = 0
// 2) Add 5
// 3) Double
// 4) Store the result

CSA rewards clear, ordered steps.

Quick self-check

  1. In one sentence: algorithm vs. program?
  2. What does the compiler do?
  3. Which error type prevents running entirely?
  4. What’s the difference between a run-time error and a logic error?
  5. What does “sequencing” mean?

← Back to Unit 1 topics