Computer ScienceChapter 3: Programming Fundamentals (Python)
3.1 Computer Program
A computer program
Explanation level
Introduction
A computer is a machine that cannot think or decide on its own. It needs clear, step-by-step instructions to perform any task. These instructions, written in a language that a computer can understand, form a computer program. Without programs, a computer is just a collection of electronic components with no useful purpose.
Definition
A computer program is a set of well-defined instructions which a computer can execute to solve a problem.
In simple words, a computer program is a collection of commands that tells the computer exactly what to do, step by step, to complete a task.
Main Idea
The program interacts with the user for some input, then executes one instruction at a time from the set of instructions (called code) and delivers the result.
In its simplest form, the program requires one algorithm to build, but large programs can contain multiple algorithms for various tasks in a single program.
Core Concept
When a program is executed:
- The program is loaded into main memory from secondary storage (like a hard disk).
- One instruction at a time is fetched from main memory.
- The instruction is provided to the processor (CPU).
- The processor executes the instruction.
It is important to keep in mind that the program itself and the data it operates upon both are kept in the main memory for faster execution.
Detailed Explanation
How a Program Executes
The process of program execution can be understood as follows:
- Disk Memory: The program is stored permanently on the disk (secondary storage).
- Main Memory (RAM): When the program runs, it is loaded into main memory. This is because main memory is much faster than disk memory.
- Processor (CPU): The processor fetches one instruction at a time from main memory, executes it, and then fetches the next instruction. This cycle continues until the program ends.
Why both program and data are in main memory: The processor needs to access both the instructions and the data they operate on. If the data were kept on disk, the processor would have to wait for the slow disk every time, making the program very slow. Keeping both in main memory makes execution fast.
Categories of Computer Programs
Generally, computer programs are categorized into two types:
1. Interactive Programs
The interactive program accepts some input which is either from a user or some other program. It responds to the user's input and continues the interaction.
- Example: A web browser. You type a URL, it fetches the page. You click a link, it loads a new page. The program keeps interacting with you.
2. Batch Programs
A batch program can be executed by a user or an interactive program. In batch programs, multiple commands are bunched together which are processed sequentially and then the program halts.
- Example: Print jobs on a printer. When you send multiple documents to print, they line up in a queue. The print server processes them one after the other and halts when no more jobs are in the queue.
Programming Languages and Translation
Computer programs are developed in programming languages which provide the development environment via a set of rules and keywords to use. With the help of keywords, you can create a set of instructions (called code) which need to follow the rules of syntax of the said programming language.
- The compiler checks whether the syntax is as per the rules.
- It converts your code into an object file.
- The object file comprises of binary data and is called machine language.
The machine language is understandable to the processor and executes your code.
- C++
- Java
- Assembly Language
- Fortran
Languages that use an interpreter: Apart from compiler, languages like Python, JavaScript, and Ruby use an interpreter. Interpreters are more flexible in the sense that the code written in high-level language is converted into machine language line-by-line, as the code executes.
Translator Programs
The translation of high-level program to low-level program is done by translator programs. There are three main types:
| Translator | How it works |
|---|---|
| Compiler | Translates the entire program at once into machine language |
| Interpreter | Translates the program line-by-line as it executes |
| Assembler | Translates assembly language into machine language |
Intermediate Level
Interactive vs Batch Programs — Comparison
| Feature | Interactive Program | Batch Program |
|---|---|---|
| Input source | User or another program | Predefined commands |
| Interaction | Continuous back-and-forth | No interaction during execution |
| Example | Web browser, chat app | Print queue, payroll processing |
| Execution | Waits for user input | Processes all commands then halts |
Compiler vs Interpreter
| Feature | Compiler | Interpreter |
|---|---|---|
| Translation | Entire program at once | Line-by-line |
| Speed of execution | Faster after compilation | Slower (translates as it runs) |
| Error detection | All errors reported together | Errors reported one at a time |
| Examples | C++, Java, Fortran | Python, JavaScript, Ruby |
| Output | Object file / machine language | Direct execution |
Syntax and Code
Syntax refers to the rules of a programming language. Just like English has grammar rules (e.g., "subject + verb + object"), programming languages have syntax rules.
Code is the set of instructions you write. If the code follows the syntax rules, it can be compiled or interpreted successfully.
Example — Syntax Error:
If the correct syntax is `print("Hello")` but you write `print("Hello"` (missing closing parenthesis), the compiler or interpreter will report a syntax error.
Formula / Mathematical Form
Not applicable for this topic.
Solved Example
Not applicable for this topic.
Important Points
- - A computer program is a set of well-defined instructions which a computer can execute to solve a problem.
- - The program interacts with the user for input, executes one instruction at a time, and delivers the result.
- - A simple program requires one algorithm; large programs can contain multiple algorithms.
- - When executed, the program is loaded into main memory.
- - One instruction at a time is fetched from main memory and provided to the processor for execution.
- - Both the program and the data it operates upon are kept in main memory for faster execution.
- - Programs are categorized into interactive and batch programs.
- - Interactive programs accept input from a user or another program (e.g., web browser).
- - Batch programs have multiple commands bunched together, processed sequentially, then halt (e.g., print jobs).
- - Computer programs are developed in programming languages that provide rules and keywords.
- - Code must follow the syntax of the programming language.
- - A compiler checks syntax and converts code into an object file (machine language).
- - Languages using compilers: C++, Java, Assembly Language, Fortran.
- - Languages using interpreters: Python, JavaScript, Ruby.
- - Interpreters convert high-level code to machine language line-by-line as the code executes.
Remember
- - Computer Program = Set of well-defined instructions.
- - Interactive Program = User input, continuous interaction (e.g., web browser).
- - Batch Program = Multiple commands, processed sequentially, then halts (e.g., print jobs).
- - Compiler = Translates entire program at once (C++, Java, Fortran).
- - Interpreter = Translates line-by-line (Python, JavaScript, Ruby).
- - Program and data both in main memory for faster execution.
- - Syntax = Rules of a programming language.
- - Object file = Output of compiler, contains machine language.
Common Mistakes
- - Thinking the computer understands English: The computer only understands machine language (0s and 1s). High-level languages need translation.
- - Confusing compiler with interpreter: A compiler translates the entire program at once; an interpreter translates line-by-line.
- - Forgetting that data also needs to be in main memory: Both the program and its data must be in main memory for fast execution.
- - Confusing interactive with batch programs: Interactive programs respond to user input; batch programs process a set of commands without interaction.
- - Ignoring syntax rules: Code that does not follow syntax rules will not compile or run.
- - Thinking all languages use compilers: Python, JavaScript, and Ruby use interpreters.
- - Assuming the program runs directly from disk: The program must first be loaded into main memory.
Quick Revision
A computer program is a set of well-defined instructions which a computer can execute to solve a problem. The program interacts with the user for some input, then executes one instruction at a time from the set of instructions (called code) and delivers the result.
When the program is executed, the program is loaded into main memory from where one instruction at a time is fetched and provided to the processor which executes it. Both the program itself and the data it operates upon are kept in the main memory for faster execution.
Computer programs are categorized into interactive and batch programs. Interactive programs accept input from a user or another program (like a web browser). Batch programs have multiple commands bunched together which are processed sequentially and then halt (like print jobs on a printer).
Computer programs are developed in programming languages which provide rules and keywords. Code must follow the syntax of the programming language. A compiler checks syntax and converts code into an object file (machine language). Languages like C++, Java, and Fortran use compilers. Languages like Python, JavaScript, and Ruby use interpreters, which convert code line-by-line as it executes.
60-Second Revision
- Computer Program = Set of well-defined instructions.
- Execution: Program loaded into main memory → instruction fetched → processor executes.
- Program + Data both in main memory for speed.
- Interactive Program = User input, continuous (web browser).
- Batch Program = Multiple commands, sequential, then halt (print jobs).
- Compiler = Entire program at once (C++, Java, Fortran).
- Interpreter = Line-by-line (Python, JavaScript, Ruby).
- Syntax = Rules of programming language.
- Warning: The processor only understands machine language (0s and 1s). High-level code must be translated.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.3.1 Computer Program
Sticky Notes · Quick Revision
Key Point 1
- Computer Program = Set of well-defined instructions.
Key Point 2
- Interactive Program = User input, continuous interaction (web browser).
Key Point 3
- Batch Program = Multiple commands, sequential, then halt (print jobs).
Key Point 4
- Compiler = Entire program at once (C++, Java, Fortran).
Key Point 5
- Interpreter = Line-by-line (Python, JavaScript, Ruby).
Key Point 6
- Program + Data both in main memory for speed.
Key Point 7
- Syntax = Rules of programming language.
Key Point 8
- Machine language = 0s and 1s, understood by processor.
Same supplied content as this Notes page (shared canonical source).