Welcome to Programming Fundamentals!

In this chapter, we are going to dive into the heart of Computer Science: Programming. This is where you learn how to give instructions to a computer to solve problems. Think of a programmer as a chef writing a recipe; if the instructions are clear and in the right order, the result is perfect. If they are messy, the "cake" (or the program) won't work!

Don't worry if some of these terms feel like a new language at first. We will break everything down into small, easy-to-manage steps.

2.2.1 The Building Blocks of Code

Variables and Constants

When we write a program, we need to store data so we can use it later. We use Variables and Constants to do this.

Analogy: Imagine a storage box.
- A Variable is a box where the contents can change. For example, a box labeled score in a game. It starts at 0 and changes to 10.
- A Constant is a box that is sealed shut once you put something in it. For example, a value for \( \pi \) (3.141) or the Gravity on Earth. It stays the same the whole time the program runs.

Assignments, Inputs, and Outputs

To make our programs interactive, we use these three actions:
1. Input: Getting data from the user (e.g., asking for their name).
2. Output: Showing data to the user (e.g., printing "Hello" on the screen).
3. Assignment: Using the = sign to give a value to a variable.
Example: score = 10 (This means "the variable score now holds the value 10").

Programming Constructs

There are three ways that a program flows. You must know these:
- Sequence: Instructions happen one after another in a specific order.
- Selection: The program makes a decision using IF statements. It’s like a fork in the road.
- Iteration: This is just a fancy word for Loops. We repeat code until a task is done.

Quick Review:
- Count-controlled loops: Repeat a set number of times (e.g., FOR i = 1 to 10).
- Condition-controlled loops: Repeat WHILE something is true (e.g., WHILE lives > 0).

Operators: The Math of Coding

You need to recognize these operators for your exam:

Arithmetic Operators:

- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- ^ (Exponentiation / To the power of)
- MOD: The remainder after division (e.g., \( 10 \text{ MOD } 3 = 1 \)).
- DIV: The whole number quotient (e.g., \( 10 \text{ DIV } 3 = 3 \)).

Comparison Operators:

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

Key Takeaway: Programs are built using Sequence, Selection, and Iteration. Use variables to store data that changes and constants for data that doesn't.

2.2.2 Data Types

Every piece of data has a "type." It tells the computer how to treat that data.
- Integer: Whole numbers (e.g., 5, -10, 0).
- Real (or Float): Numbers with a decimal point (e.g., 15.5, -0.01).
- Boolean: Only two values: True or False.
- Character: A single letter, number, or symbol (e.g., 'A', '!', '3').
- String: A collection of characters (e.g., "Hello World").

Casting

Sometimes we need to change data from one type to another. This is called Casting.
Example: If a user types "18", the computer sees it as a String. To do math with it, we must Cast it into an Integer.

Common Mistake: Forgetting that you cannot add a String to an Integer! "5" + 5 will cause an error unless you cast the string "5" into the number 5.

2.2.3 Additional Programming Techniques

String Manipulation

We can play around with strings in two main ways:
1. Concatenation: Joining two strings together (e.g., "Foot" + "ball" = "Football").
2. Slicing: Taking a "slice" out of a string (e.g., getting the first three letters of "Computer" to get "Com").

Arrays (Lists)

An Array is a static structure that stores multiple items of the same data type under one name.
- 1D Array: Like a single row of lockers. Each locker has an index (usually starting at 0).
- 2D Array: Like a grid or a spreadsheet. You need two coordinates to find an item (row and column).

Memory Aid: Always remember that computers start counting at 0, not 1!

File Handling

Programs often need to save data even after they are turned off. We use External Files for this:
- Open: Prepares the file for use.
- Read: Takes data out of the file into the program.
- Write: Puts data from the program into the file.
- Close: Saves changes and keeps the file healthy.

Sub Programs: Functions and Procedures

Instead of writing the same code 100 times, we wrap it in a Sub Program and give it a name.
- Procedures: Carry out a task.
- Functions: Carry out a task and RETURN a value back to the main program.

Scope (Local vs Global):
- Global Variables: Can be seen and used by the entire program.
- Local Variables: Are only "alive" inside the sub program they were created in. These are better for keeping code neat and avoiding mistakes!

Structured Query Language (SQL)

SQL is used to search databases. You only need to know three keywords:
- SELECT: Which fields (columns) do you want?
- FROM: Which table are you looking in?
- WHERE: What is the condition? (e.g., WHERE Price > 10)

Example: SELECT Name FROM Students WHERE Grade == 'A'

Did you know? Most of the apps you use, like Instagram or Spotify, use SQL-like languages to find your photos or songs in a massive database!

Key Takeaway: Use Sub Programs to make your code "modular" (broken into parts). Use SQL to find specific data in a table.

Final Summary Review

1. Sequence, Selection, and Iteration are the three ways code flows.
2. Variables change; Constants don't.
3. MOD gives the remainder, DIV gives the whole number.
4. Casting changes the data type (e.g., String to Integer).
5. Functions return a value; Procedures do not.
6. SQL uses SELECT, FROM, and WHERE to find data.