Algorithms, Programming, Compilers · Algoritmos, Programación, Compiladores
| English | Español |
|---|---|
| algorithm/ˈælɡərɪθəm/ | algoritmo |
| high-level language/haɪ ˈlevl ˈlæŋɡwɪdʒ/ | lenguaje de alto nivel |
| compiler/kəmˈpaɪlə/ | compilador |
| syntax error/ˈsɪntæks ˈerə/ | error de sintaxis |
| logic error/ˈlɒdʒɪk ˈerə/ | error lógico |
A clean build can still sell the wrong price
- A shop program intends to apply a 10% discount to a price of 100, but calculates price * 10 / 100 as the amount to pay.
- It calculates the discount amount, 10, instead of the discounted price, 90. The code can compile and run while implementing the wrong formula.
What is an algorithm?
- An algorithm 算法 is a finite, ordered set of steps that solves a problem. A recipe is an algorithm: precise steps, in order, that finish.
- Programming is the craft of turning an algorithm into code a computer runs. For the finite algorithms taught here, each valid input must lead to a result after finitely many steps. A program may also run an ongoing service; not every program is one terminating calculation.
Programming languages
- Computers run machine code (raw 0s and 1s) — hard for humans to write. We write in a high-level language 高级语言 like Java: readable, English-like.
- Java code says what · qué to do; a translator turns it into machine instructions. This course teaches problem-solving through Java.
Compilers
- A compiler 编译器 such as javac translates Java source files into class files and checks grammar, types and names. Java compiles to bytecode, which the Java Virtual Machine (JVM) then runs.
- "Write once, run anywhere": class files can run on different platforms with a compatible JVM and the required libraries; newer class-file versions need a runtime that supports them. Compile errors (caught before running) differ from run-time errors (caught while running).
An algorithm is best described as...
Finite, ordered steps that finish — that's an algorithm.
A Java compiler translates your source code into...
Java compiles to bytecode, which the JVM executes.
A program that compiles and runs but gives the wrong answer has a...
Wrong result despite clean compile = logic error.
A syntax error prevents a Java program from compiling.
Broken grammar → the compiler refuses to build.
If a program compiles with no errors, it is guaranteed to be correct.
The compiler checks grammar, types and names, but does not establish the intended business rule or correct output.
Every run-time exception necessarily terminates the program, even if matching exception-handling code catches it.
An exception may be caught and handled; an uncaught one can terminate execution.
Which operation throws ArithmeticException for an int variable zero whose value is 0?
Integer division by zero throws; double division by zero has IEEE floating-point outcomes.
Kinds of errors
- A syntax error 语法错误 breaks Java's grammar — the compiler refuses to build. A run-time error occurs during execution; an uncaught exception may terminate the program. Integer division by a zero variable throws ArithmeticException, while floating-point division follows different rules.
- A logic error 逻辑错误 runs fine but gives the wrong answer. Debugging is finding and fixing all three.
From source code to running program · Desde el código fuente hasta el programa en ejecución
See how Java source becomes a running program.
A program that compiles is not automatically correct. The compiler catches syntax and other compile-time errors, including type and name errors. A logic error — wrong formula, wrong condition — compiles cleanly and runs, but produces the wrong result. Always test your algorithm's output · salida, not just whether it builds.
Match each defect to its category.
The categories depend on when and how the defect is detected; successful compilation does not establish the intended result.
An algorithm to find the larger of two numbers a and · y b:
- Step 1: if
a > b, the answer isa. - Step 2: otherwise, the answer is
b. - Finite, ordered, and always finishes — a valid algorithm, ready to code in Java.
Carry the reasoning to a new case
- Check discount output against known cases. For this example, 100 should give 90 and 0 should give 0; include a non-round input under a stated rounding policy rather than assuming integer division is decimal arithmetic.
- The compiler also catches type and name errors, not only punctuation mistakes. Conversely, an exception can be handled during execution; a clean compile does not guarantee an intended result or absence of run-time failures.
An algorithm is a finite, ordered set of steps that solves a problem; programming turns it into code. Java is a high-level language that a compiler translates to bytecode for the JVM. Watch for syntax, run-time, and logic errors — a clean compile does not guarantee a correct result.