Welcome to the World of Programming!
Welcome! You are about to dive into one of the most exciting parts of Computer Science: Introduction to Programming. Think of programming as learning a new language to talk to a computer. Just like following a recipe to bake a cake, a program is simply a set of instructions that tells the computer exactly what to do.
In this chapter, we will look at Procedural Programming (the standard way of writing code) and Assembly Language (how we talk to the computer's hardware more directly). Don't worry if it seems like a lot at first—we'll break it down piece by piece!
1. Procedural Programming Techniques
Most modern languages like Python, Java, or C++ allow for Procedural Programming. This is a style of programming that uses a list of instructions to tell the computer what to do step-by-step.
Program Flow (The Three Pillars)
Almost every program ever written relies on three basic structures to control its "flow":
1. Sequence: This is the simplest one. It means the computer carries out instructions one after another, from top to bottom.
Example: 1. Wake up -> 2. Brush teeth -> 3. Eat breakfast.
2. Selection (Branching): This is where the computer makes a decision. It checks a condition (usually an IF statement) and chooses which path to take.
Example: IF it is raining, take an umbrella. ELSE, wear sunglasses.
3. Iteration (Looping): This is when the computer repeats a section of code multiple times. We use FOR loops (when we know how many times to repeat) or WHILE loops (until a condition changes).
Example: WHILE your plate is not empty, keep eating.
Variables and Constants
Imagine your computer’s memory is a giant wall of post-it notes or storage boxes.
Variables: These are "boxes" where we store data that can change while the program is running. For example, a playerScore in a game starts at 0 and goes up.
Constants: These are "boxes" for data that cannot change once the program starts. For example, PI (\( \pi \)) is always 3.14159.
Operators: The Logic Tools
To do anything useful, we need operators:
• Arithmetic Operators: Standard math like + (add), - (subtract), * (multiply), and / (divide).
• Assignment Operators: The = sign. It doesn't mean "equals" in programming; it means "take the value on the right and put it in the box on the left."
• Boolean Operators: These help with decisions. AND (both things must be true), OR (at least one must be true), and NOT (the opposite).
Procedures and Functions
Sometimes we have a block of code we want to use over and over again. Instead of typing it out ten times, we wrap it in a "subroutine."
Procedures: A block of code that performs a task but does not send a value back to the main program.
Functions: A block of code that performs a task and returns a value back.
Analogy: A Procedure is like a vacuum cleaner—it does the job but doesn't hand you anything when it's done. A Function is like a juicer—you give it fruit, and it gives you back juice.
String and File Handling
String Handling: This is how we manipulate text. We can find the length of a word, join two words together (concatenation), or take a "slice" of a word.
File Handling: Programs often need to save data permanently. We use three main steps: Open the file, Read/Write to the file, and most importantly, Close the file to save the changes.
Quick Review: Procedural programming is all about the "how." It uses variables to store data and controls the order of events using sequence, selection, and iteration.
2. Assembly Language & The Little Man Computer (LMC)
High-level languages (like Python) are easy for humans to read. However, computers only understand 1s and 0s. Assembly Language is a "low-level" language that sits right in the middle—it's a human-readable version of machine code.
What is the Little Man Computer?
The Little Man Computer (LMC) is a simplified model of a computer used to help you understand how a CPU works. Imagine a little man in a room with 100 mailboxes (memory), a calculator (the Accumulator), and a counter (the Program Counter).
Key LMC Mnemonics (Instructions)
Instead of binary, LMC uses short words called mnemonics. Here are the ones you need to know for your exam:
• ADD: Add the value in a mailbox to the calculator (Accumulator).
• SUB: Subtract the value in a mailbox from the calculator.
• STA: Store the value from the calculator into a mailbox.
• LDA: Load a value from a mailbox into the calculator.
• INP: Take an Input from the user.
• OUT: Output the current value in the calculator.
• HLT: Halt (Stop) the program.
• BRA: Branch Always (Jump to a different instruction).
• BRZ / BRP: Branch if Zero / Branch if Positive (Jump only if the calculator is 0 or positive).
Example of a Simple LMC Program
Let's look at a program that adds two numbers together:
1. INP (Get first number)
2. STA FIRST (Store it in a mailbox labeled 'FIRST')
3. INP (Get second number)
4. ADD FIRST (Add the first number to the second number)
5. OUT (Show the result)
6. HLT (Stop)
Did you know? Even though we write in high-level languages today, assembly is still used for things like programming fast graphics cards or tiny medical devices because it gives the programmer total control over the hardware!
3. Common Pitfalls and Tips
Don't worry if this seems tricky at first! Many students get confused by these specific things:
• = vs ==: In many languages, a single = puts a value in a box (Assignment), while a double == checks if two things are the same (Comparison).
• Infinite Loops: Always make sure your WHILE loop has a way to end, or your program will get stuck forever!
• Function Returns: Remember that a Function must always use the word return to send its answer back. If you forget, the result disappears!
• LMC Memory: In LMC, remember that the STA and LDA commands change the mailboxes or the calculator, but they don't delete what was there before—they just overwrite it!
Key Takeaways for Revision
• Procedural code is a sequence of steps.
• Selection is choosing (IF), Iteration is repeating (WHILE/FOR).
• Variables change; Constants don't.
• Functions return a value; Procedures do not.
• LMC uses mnemonics like ADD, SUB, STA, LDA to represent low-level instructions.
• Branching in LMC (BRA, BRZ, BRP) is how we create IF statements and loops in Assembly.