TOPIC 1.1 · STUDY

Introduction to Algorithms, Programming, and Compilers

An algorithm is a sequence of steps that finishes a task. A Java program is written as source code, compiled, and then run. The compiler catches syntax errors before a new program can run. Logic errors and run-time errors show up later, when you test.

1.1.A

Algorithms

An algorithm is a finite list of steps that accomplishes a task. Recipes, directions, and checklists are algorithms. You can write the steps in words or draw them. Someone else should be able to follow them without guessing what you meant.

The steps run in the order given. That order is part of the algorithm. Doing a later step first can make the task impossible or produce a different result.

Three steps in order: fill the watering can, pour until the soil is damp, then empty the leftovers into the sink. Arrows point from each step to the next.

Each arrow means the next step starts only after the previous one finishes.

Those three steps water one plant. Empty the can before you pour, and the soil stays dry. “Water the plant the right way” is not an algorithm: it names a goal and leaves out the steps.

Key points

  • An algorithm is specific steps, in a specified order, that finish a task.
  • Words and diagrams are both valid ways to describe those steps.
  • Changing the order can change the result.
  • A goal by itself is not an algorithm.

1.1.B

Writing, compiling, and running a program

A program starts as source code: text written in a programming language. In this course the language is Java. You can type that text in a plain text editor or in an IDE (integrated development environment).

An IDE combines tools you could also run separately, usually an editor, a compiler, and a way to run the program. An IDE is convenient. It is not required. A text editor plus a separate compiler is a complete way to write a program.

A text editor and an IDE both point to the same result, source code. The IDE is one way to write the program, not the only way.

Both paths produce source code. The IDE bundles tools. It does not change what the source is.

Before a new program runs, a compiler translates the source into a form the computer can execute. The compiler also reports errors that prevent a successful translation. Those errors have to be fixed, and the source compiled again, before you run the new program. Running an older translated copy does not test the edits you just made.

Three stages in a row: write the source, compile it, then run it. A syntax error under compile says to fix the source before this run. A note under run says a clean compile can still be wrong.

The top row is how a new program gets from text to a run. A compiler error stops that path. A successful compile still needs a test.

A successful compile means the compiler found no error that blocked translation. It does not mean the algorithm is correct, and it does not mean every run will finish normally. You still test: choose an input, decide what the algorithm should produce, run the new program, and compare.

Key points

  • Source code can be written in a text editor or an IDE.
  • An IDE bundles tools. It does not replace testing, and it is not mandatory.
  • The compiler translates source and reports errors that block that translation.
  • Fix compiler errors before you run the new program.
  • A clean compile is not proof that the program is correct.

1.1.C

Syntax errors, logic errors, and run-time errors

These three errors show up at different times. The time is how you tell them apart.

Three columns. Syntax error: while compiling, the program does not start, a language rule is broken. Logic error: while testing, the run finishes, the result is wrong. Run-time error: during execution, the program had compiled, then that run fails.

Classify the error by when it appears, not by how serious it feels.

A syntax error breaks a rule of the language. The compiler rejects the source, and the new program does not start. A person can often still guess what the line was meant to say. The compiler does not guess.

System.out.println("Open the greenhouse")

Java requires a semicolon at the end of that statement. The compiler stops on the missing semicolon. Nothing runs. Adding the semicolon fixes the syntax. It still does not prove that “Open the greenhouse” is the right message for the algorithm.

A logic error is a mistake in the steps or in how they were written down. The program compiles and runs to the end, but the result does not match the algorithm. If the steps say to add 4 and 5 and the program multiplies them, the run can finish and print 20. You expected 9. Testing catches that: compare the actual output with a result you computed by hand.

A run-time error, often reported as an exception, happens while the program is executing. The source compiled. Then something during that run fails, such as a division by zero, and the run stops. That is not a logic error. A logic error still produces a result. A run-time error interrupts the run. It is also not a syntax error, because the compiler already accepted the program.

Error When it appears What you observe Example
Syntax error Compilation The compiler rejects the source. The new program does not run. A missing semicolon, a misspelled keyword, or a brace that does not match.
Logic error Testing, after a completed run The program finishes, and the result disagrees with the algorithm. The steps say to add, and the program multiplies.
Run-time error During execution The program compiled, then that run failed. Dividing by zero while the program is running.

Key points

  • Syntax errors are caught by the compiler. The new program does not run until they are fixed.
  • Logic errors are caught by testing. The program runs, and the result is wrong.
  • Run-time errors are caught during execution. The program compiled, then that run failed.
  • Compilation does not check that the algorithm solves the problem.

Do not treat these as the same event. A compiler message is a syntax problem, not evidence that the algorithm is wrong. Output that simply disagrees with the expected result is a logic error. An exception during a run is a run-time error. Classify the error by when it appears.

Practice this topic