1.1 Algorithms, programming and compilersFrom an idea to a running program

An algorithm is a step-by-step process that solves a problem or finishes a task. You already follow algorithms all day. A recipe is one. So is "to get to school: walk two blocks, turn left at the bakery, cross at the light." The steps happen in a fixed order, and that order matters. Doing the steps one after another, top to bottom, is called sequencing, and it's the first of three building blocks every algorithm is made of. The other two, selection (choosing) and iteration (repeating), are Unit 2.
A program is an algorithm written in a programming language, here Java. A computer is fast but completely literal: it does exactly what you wrote, never what you meant.
What happens when you press Run
You write Java as source code, in a file whose name ends in .java. Before the program can run, a program called the compiler reads all of your source code and checks it against Java's rules. If it finds a problem, it stops and shows you an error message. If everything follows the rules, it translates your code into bytecode, a compact form the Java Virtual Machine (the JVM) can run. The JVM is what actually runs your program and produces the output. An IDE (integrated development environment) is the app that bundles all of this: an editor, the compiler and a Run button.
?Wait, what's a class? We haven't learned that yet.
Here's a complete Java program. Read it line by line. The braces { } mark where the class and the main method begin and end, and each statement ends with a semicolon.
1public class Greeting12{3 public static void main(String[] args)24 {5 System.out.print("Hi, ");36 System.out.println("Mr. T");47 System.out.println("Line two");48 }9}
- line 1 Every Java program lives inside a class
- line 3 The program starts running here, at main
- line 5 print stays on the same line
- line 6-7 println prints, then moves to a new line
Three kinds of errors
A syntax error (or compile-time error) breaks the rules of the language, like a missing semicolon or a misspelled keyword. The compiler catches it, so nothing runs. A run-time error happens while the program runs: the code is legal, but something impossible happens, like dividing a whole number by zero. Java stops and reports an exception, here an ArithmeticException.
A logic error is the sneaky one. The program runs to the end, but the answer is wrong, and nothing warns you. The only way to find it is testing: running the program on inputs whose answers you already know, including unusual ones like 0, negative numbers and empty text.
| Error | When it shows up | Example | What you see |
|---|---|---|---|
| Syntax (compile-time) | Before the program runs | int x = 4.5; or a missing ; | An error message, no program |
| Run-time (exception) | While the program runs | int share = 12 / people; with people 0 | ArithmeticException, program stops |
| Logic | Never announced | area = 1 / 2 * b * h; gives 0 | It runs and prints a wrong answer |
?Wait, what's an exception again?
I thought about it. Show me
Each segment below is meant to compute something about splitting 12 slices of pizza. For each one, say whether it has a syntax error, a run-time error, a logic error, or no error.
Segment I int slices = 12 int people = 4; Segment II int slices = 12; int people = 0; int share = slices / people; Segment III double base = 6; double height = 4; double area = 1 / 2 * base * height;
- Segment I: the first line has no semicolon at the end. Java requires one after every statement, so the compiler stops. That's a syntax error.
Why?
The compiler reads the whole file first. Even one missing semicolon means nothing runs. - Segment II: every line follows the rules, so it compiles. But when the third line runs, people is 0, and dividing an int by 0 is impossible. Java throws an ArithmeticException. That's a run-time error.
Why?
The compiler doesn't know what value people will hold when the line runs. It only checks the rules, so the problem shows up while the program runs. - Segment III: it compiles and runs. But 1 / 2 divides two whole numbers, which gives 0 (you'll see why in 1.3), so the area is 0.0 instead of 12.0. No message appears. That's a logic error.
Why?
The rubric idea: a logic error is any program that runs to the end and gives an answer that doesn't match what it was supposed to compute.
Answer: I: syntax error. II: run-time error (ArithmeticException). III: logic error (area is 0.0, not 12.0).
Stuck? Get a hint
- Ask the questions in order: does it follow Java's rules? If not, it's a syntax error.
- If it compiles, does something impossible happen while it runs, like dividing by zero?
- If it runs to the end, is the answer what the programmer wanted? If not, it's a logic error.
- AIt will produce the correct output for every input.
- BIt follows Java's syntax rules, but it may still crash or give wrong answers.
- CIt cannot throw an exception while it runs.
- DIt has been tested on all possible inputs.
Show the answer
B. Compiling checks only the rules of the language. The program can still throw an exception at run time (choice C is the trap) or contain a logic error, so choices A, C and D all claim more than compiling can promise.
- An algorithm is a step-by-step process. Sequencing means the steps run in order, top to bottom.
- The compiler checks your source code and translates it to bytecode. The JVM runs the bytecode.
- Syntax error: caught by the compiler, nothing runs. Run-time error: an exception stops the program. Logic error: it runs and gives a wrong answer.
- Test with inputs whose answers you know. That's the only way to catch logic errors.