1.1 - Introduction to Algorithms, Programming, and Compilers
1.1 Introduction to Algorithms, Programming, and Compilers
Algorithms are just step by step processes you follow to complete a task or solve a problem. You can represent them using plain written language or with diagrams, there’s no single required format.
For example, here’s an everyday algorithm for making a bowl of cereal, written in plain language:
- Get a bowl and a spoon.
- Pour cereal into the bowl.
- Pour milk over the cereal.
- Eat it with the spoon.
The same steps could also be drawn as a flowchart, a diagram where each box is a step and arrows show what comes next. Either way, it’s the same algorithm.
Sequencing means the steps in a process happen in a specific order, one at a time. Nothing runs simultaneously here, it’s one step, then the next.
In Java, sequencing means statements run from top to bottom, one at a time. Changing the order of the steps can change the result:
int x = 5; x = x + 2; // x is now 7 x = x * 3; // x is now 21 // the same two steps in the opposite order int y = 5; y = y * 3; // y is now 15 y = y + 2; // y is now 17
You can write code in literally any text editor, but most people use an IDE (integrated development environment) because it gives you tools to write, compile, and run your code all in one place.
A compiler checks your code for certain errors before it can run. If it finds one of those errors, you have to fix it first, the program just won’t run otherwise.
Here’s the whole process in Java: you write source code in a
.java file, the compiler checks it and translates it into bytecode (a .class file), and then the Java Virtual Machine (JVM) runs that bytecode. If the compiler finds an error, the program can’t run until you fix it.Types of Errors
There are three main categories you need to know:
- Syntax error: you broke a rule of the language itself, like forgetting a semicolon. The compiler catches these.
- Other common ones: a missing closing brace or parenthesis, or a string that’s missing its closing quotation mark.
- Logic error: your code runs, but it does the wrong thing. These don’t get caught by the compiler, you find them by testing the program with specific inputs and checking if the output matches what you expected.
- Run-time error: something goes wrong while the program is actually executing, and it usually causes the program to crash or terminate abnormally.
- An exception is a specific kind of run-time error, one the compiler didn’t catch, that interrupts the normal flow of the program while it’s running.
- Exceptions you’ll see in this course include
ArithmeticException(dividing anintby zero, 1.3),NullPointerException(calling a method onnull, 1.14),StringIndexOutOfBoundsException(a bad string index, 1.15), andArrayIndexOutOfBoundsException(a bad array index, Unit 4).
The programming language for this class is Java.
// Example of a syntax error (missing semicolon) int x = 5 // Example of a logic error (meant to add, but multiplied instead) int total = 5 * 3; // should have been 5 + 3 // Example of a run-time error / exception int result = 10 / 0; // throws an ArithmeticException while running
Quick way to tell them apart:
- Won’t compile: a syntax error (the compiler catches it).
- Compiles, but crashes while running: a run-time error, like an exception.
- Runs all the way through, but gives the wrong result: a logic error.