Welcome to the World of Code!
Welcome to one of the most exciting parts of Computer Science: Programming Concepts. Think of this chapter as your "toolbox." Just like a builder needs a hammer, nails, and a blueprint to create a house, a programmer needs variables, loops, and logic to create software. We are focusing on Procedural Programming, which is simply a way of writing code as a list of step-by-step instructions for the computer to follow. Don't worry if some of this feels like learning a new language—by the end of these notes, you’ll be speaking "computer" fluently!
1. The Basics: Storing and Using Data
Before we can do anything complex, we need to know how to handle information.
Variable Declaration and Assignment
Imagine a variable as a physical box in your room. You put a label on the box (the identifier) so you know what's inside, and you store a value in it.
- Variable Declaration: Telling the computer "I need a box called 'Score' to hold a number."
- Assignment: Putting something in the box. For example: Score = 10.
Meaningful Identifiers
You could name your variable x, but user_age is much better. Why? Because if you look at your code six months from now, you’ll know exactly what user_age does! Using meaningful identifiers makes your code readable and easier to fix.
Input and Output
Programs need to talk to people!
- Output: Displaying information on the screen (e.g., "Game Over!").
- Input: Getting information from the user (e.g., asking for their name).
Comments: Notes for Humans
Comments are lines of text that the computer completely ignores. We use them to explain what a tricky bit of code is doing. It's like leaving a sticky note for yourself or your teammates.
Quick Tip: In many languages, comments start with # or //.
Key Takeaway: Variables are labeled boxes for data, and clear names plus helpful comments make your code "human-friendly."
2. Mathematical and Logical Tools
Computers are essentially super-fast calculators. To use that power, we use operators.
Arithmetic Operations
Most of these you know from math class, but a few are special in programming:
- Addition (+), Subtraction (-), Multiplication (*).
- Real/Float Division (/): Standard division that gives a decimal (e.g., \( 7 / 2 = 3.5 \)).
- Integer Division (DIV): Tells you how many whole times a number goes into another (e.g., \( 7 \text{ DIV } 2 = 3 \)).
- Remainder (MOD): Tells you what is left over (e.g., \( 7 \text{ MOD } 2 = 1 \)).
- Exponentiation: Raising a number to a power (e.g., \( 3^2 = 9 \)).
- Rounding vs. Truncation: Rounding finds the nearest whole number. Truncation simply cuts off the decimal part without rounding up (e.g., 3.9 becomes 3).
Relational Operations
These are used to compare two things. The result is always True or False.
- Equal to (==) and Not equal to (!=)
- Less than (<) and Greater than (>)
- Less than or equal to (<=) and Greater than or equal to (>=)
Boolean Operations (Logic)
This is how computers make decisions. You need to know the Order of Precedence (which one happens first):
1. NOT (Highest - flips the value: NOT True = False)
2. AND (True only if BOTH sides are True)
3. OR (Lowest - True if AT LEAST ONE side is True)
- XOR: Short for "Exclusive Or." This is True only if exactly one side is True (but not both!).
Memory Aid: Remember N.A.O. (NOT, then AND, then OR) to keep your logic in order!
Key Takeaway: DIV gives the "whole" answer, MOD gives the "leftover," and Boolean logic (NOT, AND, OR) is the brain of your program.
3. Controlling the Flow: Selection and Iteration
A program doesn't always run in a straight line. Sometimes it needs to choose a path or repeat a task.
Selection (The "If" Statement)
This allows the program to choose between different paths. If it is raining, take an umbrella; Else, wear sunglasses.
Nested Selection is just an "if" statement inside another "if" statement. Imagine a decision tree: First, check if the user is logged in. If yes, then check if they are an admin.
Iteration (Loops)
Iteration is just a fancy word for repeating something. There are two types:
- Definite Iteration (FOR loops): Use this when you know exactly how many times you want to repeat (e.g., "Run 10 laps").
- Indefinite Iteration (WHILE loops): Use this when you want to repeat until something changes (e.g., "Keep running UNTIL you are tired").
Did you know? A nested iteration is a loop inside a loop. Think of a clock: the "minutes" loop has to go around 60 times before the "hours" loop moves once.
Key Takeaway: Selection is for choices; Iteration is for repeating tasks. Use "For" when you know the count, and "While" when you don't.
4. String-Handling and Conversions
A string is just a sequence of characters (like "Hello World"). We can manipulate them in several ways:
- Length: Counts the characters (including spaces!).
- Position: Finding where a character is. Warning: Most languages start counting at 0!
- Substring: Taking a "slice" out of a string (e.g., taking "Gold" out of "Golden").
- Concatenation: Gluing two strings together (e.g., "Apple" + "Pie" = "ApplePie").
Type Conversions (Casting)
Sometimes you have the number "5" as a string, but you need it as an Integer to do math. Changing data from one type to another is called casting. You must be able to convert between strings, integers, floats, and dates.
Key Takeaway: Strings are text. Remember that computers usually start counting their position at 0, not 1!
5. Subroutines and Scope
As programs get bigger, they get messy. Subroutines (also called functions or procedures) help us stay organized.
What is a Subroutine?
A subroutine is a named block of code that performs a specific task. Instead of writing the same 20 lines of code five times, you write it once in a subroutine and "call" its name whenever you need it.
Parameters and Return Values
- Parameters: The information you "pass" into the subroutine so it can do its job.
- Return Values: The result the subroutine sends back to the main program.
Local vs. Global Variables
- Global Variables: These live outside and can be seen/used by the entire program. They are like a public park.
- Local Variables: These live only inside a specific subroutine. Once the subroutine finishes, the variable is deleted. They are like your private bedroom.
Pro-Tip: It is better to use Local variables whenever possible to avoid accidental bugs!
The Stack Frame
When a subroutine is called, the computer uses a Stack Frame to keep track of:
1. The Return Address (where to go back to in the main program).
2. The Parameters being used.
3. The Local Variables for that specific call.
Key Takeaway: Subroutines are mini-programs that keep code tidy. Local variables are safer because they stay tucked away inside their subroutines.
6. Handling the Unexpected
No matter how good a programmer you are, things go wrong. A user might enter a word when the program expects a number, or a file might be missing.
Exception Handling
Instead of the program simply "crashing," we use Exception Handling. This is like a safety net. We tell the computer: "Try to do this task, but if an error (exception) happens, do this other thing instead of crashing."
Random Number Generation
Sometimes we want the computer to be unpredictable, like rolling a die in a game. Most languages have a built-in tool for Random Number Generation. You’ll use this to add variety to your programs.
Quick Review:
- DIV/MOD: Whole division vs. Remainder.
- Boolean Precedence: NOT -> AND -> OR.
- Iteration: "For" (known) vs. "While" (unknown).
- Scope: Local (inside) vs. Global (outside).
- Exception Handling: The program's safety net.
Don't worry if this seems like a lot to remember. Programming is a practical skill—the more you practice writing these structures in actual code, the more natural they will feel. Happy coding!