Unit 1: Introduction to Programming
1. Introduction to Computer Programs
A computer program is a sequence of instructions, written in a specific programming language, that a computer can execute to perform a task. Think of it as a detailed recipe that a computer follows precisely.
2. Programming Languages
Natural Language vs. Programming Language
Natural Languages (like English, Hindi, Bengali) are what humans use to communicate. They are complex, ambiguous, and evolve over time.
Programming Languages (like C, Java, Python) are formal, structured languages used to communicate instructions to a computer. They are unambiguous and have strict syntax rules.
Machine Level Language (1st Generation)
- This is the "natural language" of the computer's CPU.
- It consists purely of binary code (1s and 0s).
- It is extremely fast to execute but almost impossible for humans to read or write.
- It is machine-dependent (a program for one CPU won't work on another).
Assembly Level Language (2nd Generation)
- This language uses short, English-like mnemonics (e.g.,
MOV, ADD, JMP) to represent machine-level instructions.
- It is easier to read than machine code but is still very low-level.
- It must be translated into machine code by a program called an Assembler.
- It is also machine-dependent.
High-level Language (3rd Generation)
- These languages (like C, FORTRAN, Pascal, Python, Java) use English-like words and mathematical notation.
- They are focused on problem-solving rather than computer hardware.
- They are machine-independent (portable) and must be translated using a Compiler or Interpreter.
3. Language Translators
Computers only understand machine code. A translator is a program that converts high-level or assembly language into machine code.
Compiler
A compiler reads the *entire* source code at once, translates it, and creates a separate executable file (e.g., an .exe file).
- Process: Source Code -> Compiler -> Object Code -> Linker -> Executable File.
- Reports all syntax errors at the end of compilation.
- Result: Fast execution speed (once compiled).
- Example: C, C++, Java (partially).
Interpreter
An interpreter reads the source code one line at a time, translates that line, and executes it immediately.
- Process: Source Code -> Interpreter -> Direct Execution (no separate file).
- Stops execution as soon as it finds an error.
- Result: Slower execution speed (must be translated every time) but easier for debugging.
- Example: Python, JavaScript, old BASIC.
4. Key Programming Terms
- Source Code: The human-readable program written in a high-level language.
- Target Code (or Object Code): The machine-level code produced by a compiler.
- Input: Data provided to the program (e.g., from the keyboard).
- Output: Data provided *by* the program (e.g., to the screen).
- Compiling: The act of translating source code into target code.
- Warning: A message from the compiler about a potential, but not critical, issue (e.g., "variable declared but not used"). The program will still compile.
- Running: Executing the compiled program.
- Debugging: The process of finding and fixing errors (bugs) in a program.
- Testing: The process of running a program with various inputs to verify that it works correctly.
5. Errors in Computer Programs
There are three main types of errors in programming:
- Syntax Errors (Compile-time Errors):
- Errors in the grammar of the programming language.
- Examples: Forgetting a semicolon (
;), misspelling a keyword (whlie instead of while).
- These are caught by the compiler, and the program will not run.
- Runtime Errors:
- Errors that occur *while* the program is running.
- Examples: Dividing a number by zero, trying to access an invalid memory location.
- These errors will cause the program to crash or behave unexpectedly.
- Logical Errors:
- The program runs successfully but produces the wrong output.
- The "grammar" is correct, but the "meaning" (logic) is flawed.
- Example: Using
(a + b) / 2 to calculate the average but writing a + b / 2 (which is a + (b/2)).
- These are the most difficult errors to find and fix.