Lecture 1: Introduction to programming
What Is Programming?
Programming is the process of writing instructions for a computer to execute.
It involves problem-solving, logic, and creativity.
Why Learn Programming?
- It's essential in fields such as software development, data science, AI, and automation.
- It enhances logical thinking and problem-solving skills.
Problem Solving
Solving problems is at the core of computer science.
A programmer must:
- Understand how a human solves a problem.
- Translate this solution into a computer algorithm.
- Write the correct syntax for the computer to execute.
Sometimes, a computer solves a problem differently from a human.
Stages of Problem Solving
- Analyze the problem.
- Write the algorithm steps:
- Pseudocode
- Flowchart
- Write the program in a programming language.
- Translate the program into machine code.
- Execute, test, and debug the program.
- Document the software.
Problem Elements Analysis
Analyzing the problem involves identifying:
- Inputs: Required data and their format.
- Processing: Necessary mathematical and logical operations.
- Outputs: Desired results and their format.
Key Questions
- What are the desired results or outputs?
- What data (inputs) are available?
- What processes will convert inputs into outputs?
Algorithms
Definition
An algorithm is a well-defined set of rules or steps for solving a problem in a finite number of steps.
Characteristics of a Good Algorithm
- Each step must be clear and unambiguous.
- The algorithm must terminate after a finite number of steps.
- The process must lead to the correct solution.
Characteristics of an Algorithm
- Well-Defined Inputs and Outputs: Clear input values and definite output.
- Definiteness: Each step is precise and unambiguous.
- Finiteness: Must terminate after a finite number of steps.
- Effectiveness: Steps must be simple and executable within reasonable time and resources.
- Correctness: Produces accurate results for the problem.
- Generality: Applicable to a range of similar problems.
- Sequential Execution: Steps follow a logical order.
Algorithm Design Techniques
Algorithms can be represented by:
- Flowcharts: Graphical representation of logical steps.
- Pseudocode: English-like representation of algorithm steps.
Pseudocode
Definition
Pseudocode is a simplified, language-like representation of an algorithm that focuses on logic, not syntax.
Features
- Focuses on problem-solving steps.
- Can be implemented in any programming language.
- Easier to understand and modify before coding.
Components
- Keywords
- Sections
- Statements
Writing Pseudocode
Guidelines
- Use meaningful variable names.
- Keep instructions simple, clear, and unambiguous.
Advantages
- Easy to understand.
- No need to memorize symbols (unlike flowcharts).
- No strict syntax.
- Easy to convert to code.
- Space-efficient.
- Based on simple, clear rules.
Disadvantages
- Can become lengthy for complex problems.
Statement Structures in Pseudocode
1. Sequential Statements
Executed one after another.
Example: Add two numbers
BEGIN
INPUT N1, N2
SUM = N1 + N2
OUTPUT "The sum is: ", SUM
END2. Decision (Selection) Statements
Used when the program must choose between alternatives based on a condition.
Example: Division with validation
BEGIN
INPUT number1, number2
IF number2 ≠ 0 THEN
result = number1 / number2
OUTPUT "The result is: ", result
ELSE
OUTPUT "Error: Division by zero is not allowed."
ENDIF
END3. Repetition (Looping) Statements
Used to repeat instructions multiple times until a condition is false.
Example: Print "Welcome" five times
BEGIN
SET counter = 1
WHILE counter ≤ 5 DO
OUTPUT "Welcome"
counter = counter + 1
ENDWHILE
ENDNote: The repetition structure can be implemented using loops such as while, for, or do-while.
Summary
- Programming involves writing logical instructions for a computer.
- Problem-solving is the foundation of programming.
- Algorithms provide structured solutions.
- Pseudocode and flowcharts help design algorithms before implementation.
- Key control structures: Sequence, Decision, and Repetition.