Welcome to the Core of Programming!

In this chapter, we are going to explore the building blocks of procedural programming. Think of a program like a recipe for a cake: you have ingredients (data), instructions on what to do (statements), and ways to handle surprises (exceptions). By the end of these notes, you’ll understand how to "talk" to a computer using the basic structures that every programmer needs to know. Don't worry if it seems like a lot—we'll take it one step at a time!


1. The Building Blocks: Statement Types

In procedural programming, we give the computer a series of instructions. There are five main types of statements you need to know:

Key Statement Types

1. Variable Declaration: This is like telling the computer, "Hey, save a spot in your memory for a value and call it 'Score'."
2. Assignment: Giving a value to a variable. For example: Score = 10.
3. Selection: Making a choice. If a condition is true, do one thing; otherwise, do something else (using IF or CASE statements).
4. Iteration: Repeating a block of code (using loops).
5. Subroutine: A named block of code that performs a specific task. You can "call" it whenever you need it.

Input and Output

A program isn't very useful if it can't talk to you!
- Output: Displaying information on the screen.
- Input: Obtaining information from the user via the keyboard.

Identifier Names and Comments

When you name a variable, use meaningful identifier names. Instead of naming a variable x, name it user_age. This makes your code easier to read for humans!
Similarly, use comments. These are notes for humans that the computer ignores. They explain why you wrote a certain piece of code.

Quick Review Box:
- Use meaningful names (e.g., totalPrice instead of t).
- Use comments to explain complex logic.
- Selection = Choosing. Iteration = Repeating.


2. Arithmetic Operations

Computers are essentially very fast calculators. You need to be familiar with these operations:

- Addition (+) and Subtraction (-).
- Multiplication (*) and Real/Float Division (/): This gives the full answer, e.g., \( 7 / 2 = 3.5 \).
- Integer Division (DIV): This tells you how many whole times a number goes into another. E.g., \( 7 \text{ DIV } 2 = 3 \).
- Remainder (MOD): This tells you the remainder left over after division. E.g., \( 7 \text{ MOD } 2 = 1 \).
- Exponentiation (Power): Raising a number to a power (e.g., \( 2^3 = 8 \)).
- Rounding: Changing a number to the nearest whole number or decimal place.
- Truncation: Simply cutting off the decimal part of a number without rounding.

Memory Aid:
Think of MOD as "The Leftovers." If you have 7 cookies and share them between 2 people, each gets 3 (that's DIV) and 1 cookie is left over (that's MOD)!


3. Relational and Boolean Operations

Selection statements (like IF) rely on Relational Operators to ask questions:

- Equal to (= or ==)
- Not equal to (!= or <>)
- Less than (<)
- Greater than (>)
- Less than or equal to (<=)
- Greater than or equal to (>=)

Boolean Logic (AND, OR, NOT, XOR)

Sometimes you need to combine questions.
- AND: Both parts must be true.
- OR: At least one part must be true.
- NOT: Reverses the result (True becomes False).
- XOR (Exclusive OR): One is true, but not both.

Order of Precedence

When you have a long line of logic, the computer follows a specific order:
1. NOT (Highest precedence)
2. AND
3. OR (Lowest precedence)

Common Mistake to Avoid: Thinking OR is exclusive. In programming, if both sides are true, OR is still true. If you want it to be only one or the other, use XOR!


4. String-Handling Operations

A string is just a sequence of characters (like "Hello World"). You need to know how to manipulate them:

- Length: Counting the characters in a string.
- Position: Finding where a specific character is located.
- Substring: Extracting a part of a string (e.g., taking "Gold" out of "Golden").
- Concatenation: Joining two strings together (e.g., "Foot" + "ball" = "Football").
- Character to Code: Finding the numeric code (like ASCII) for a character.
- Code to Character: Turning a numeric code back into a character.

Type Conversion (Casting)

Often you need to change the data type so you can work with it. For example, if you get "10" from a user, it’s a string. You must convert it to an integer before you can add numbers to it!


5. Advanced Control Structures

Iteration (Loops)

There are two main ways to repeat code:
- Definite Iteration: You know exactly how many times it will run (e.g., a FOR loop).
- Indefinite Iteration: You keep going until a certain condition changes (e.g., a WHILE loop).

Nested Structures

You can put a selection inside another selection (Nested Selection) or a loop inside another loop (Nested Iteration).
Analogy: Think of a clock. The minute hand loops 60 times (inner loop) for every single time the hour hand moves once (outer loop).

Exception Handling

Sometimes things go wrong (like trying to divide by zero). Exception handling allows your program to "catch" the error and deal with it gracefully instead of just crashing.


6. Subroutines and Scope

A subroutine is a named "mini-program" inside your main program.
Advantages: Code is easier to read, you don't have to write the same thing twice, and it's easier to test.

Parameters and Returns

- Parameters: Data you pass into a subroutine so it has something to work with.
- Return Values: The result the subroutine sends back to the main program.

Local vs. Global Variables

- Global Variables: Can be accessed from anywhere in the whole program.
- Local Variables: Only exist inside the specific subroutine where they were created.

Did you know? It is much better practice to use local variables. It prevents subroutines from accidentally changing data they shouldn't touch elsewhere in the program!

Stack Frames

When a computer calls a subroutine, it uses a stack frame to keep track of everything. It stores:
1. The return address (where to go back to when the subroutine finishes).
2. The parameters passed to it.
3. The local variables used inside it.


Key Takeaways

- Procedural programming uses sequence, selection, and iteration.
- DIV is for whole numbers; MOD is for the remainder.
- Boolean order: NOT, then AND, then OR.
- Subroutines help organize code; Local variables keep it safe.
- Stack frames are the computer's way of remembering where it was before it started a subroutine.