Welcome to the Building Blocks of Programming!

In this chapter, we are diving into the most fundamental parts of coding: Variables, Data Types, and Operators. Think of these as the "nouns" and "verbs" of your code. Whether you are learning Java or Python for your Paper 2 exam, these concepts are the foundation for everything else you will build.

Don’t worry if code seems intimidating right now. By the end of these notes, you will see that programming is really just about giving the computer very specific instructions using simple tools.

1. Variables: Your Digital Storage Boxes

Imagine you are moving to a new house. You have a lot of boxes, and you put labels on them like "Kitchen Supplies" or "Books." In programming, a variable is exactly like one of those labeled boxes. It is a space in the computer's memory used to store a piece of information that can change (or "vary") while the program is running.

Key Rules for Variables:
1. Declaration: Telling the computer "I need a box" and giving it a name.
2. Assignment: Putting a value into the box (usually using the \( = \) sign).
3. Naming: Variable names should be descriptive. Instead of naming a variable \( x \), call it \( studentAge \) or \( total_price \).

Analogy: If you are playing a video game, the Score is a variable. It starts at \( 0 \), and every time you do something good, the computer "updates" that variable to a new number.

Quick Review: Variable Best Practices

To keep your code readable for IB examiners (and yourself!), avoid starting variable names with numbers and don't use spaces. Most programmers use camelCase (e.g., \( userScore \)) or snake_case (e.g., \( user_score \)).

2. Data Types: What’s Inside the Box?

Computers are powerful, but they aren't very smart—they need to know exactly what kind of data they are holding. If you try to "add" a word to a number, the computer will get confused! We use Data Types to tell the computer how to treat the data.

Here are the essential data types you need to know for the IB syllabus:

A. Integer
These are whole numbers with no decimals. They can be positive, negative, or zero.
Examples: \( 10 \), \( -5 \), \( 1000 \)

B. Real / Float
These are numbers that include a decimal point. In Java, they are often called double; in Python, they are float.
Examples: \( 3.14 \), \( 99.99 \), \( -0.5 \)

C. String
A "string" of characters. This is how we store text. Strings are almost always wrapped in quotation marks.
Examples: "Hello World", "Computer Science", "123" (Note: "123" is text here, not a number!)

D. Boolean
The simplest data type. It can only have two possible values: True or False. This is used for logic and decision-making.
Example: \( isGameOver = False \)

E. Character
A single letter, number, or symbol.
Examples: 'A', '!', '4'

Key Takeaway:

Integers and Reals are for math. Strings and Characters are for text. Booleans are for yes/no logic.

3. Operators: Making Things Happen

Operators are symbols that tell the computer to perform a specific action. We group them into three main categories.

Arithmetic Operators (The Math Experts)

These are used to perform calculations:

- Addition: \( + \)
- Subtraction: \( - \)
- Multiplication: \( * \)
- Division: \( / \)
- Modulus (MOD): Written as \( % \) in code. This gives you the remainder of a division. (e.g., \( 7 \text{ MOD } 3 = 1 \))
- Integer Division (DIV): This divides numbers but throws away the remainder to give a whole number. (e.g., \( 7 \text{ DIV } 3 = 2 \))

Comparison (Relational) Operators

These are used to compare two values. The result is always a Boolean (True or False).

- Equal to: \( == \) (Note the double equals sign!)
- Not equal to: \( != \)
- Greater than: \( > \)
- Less than: \( < \)
- Greater than or equal to: \( >= \)
- Less than or equal to: \( <= \)

Logical Operators

These allow us to combine multiple conditions together. In IB Computer Science notation, we write these in all caps:

- AND: True only if both sides are true. (e.g., I have money AND the store is open).
- OR: True if at least one side is true. (e.g., It is Saturday OR I am on vacation).
- NOT: Reverses the value. (NOT True becomes False).

Common Mistake Alert: A single \( = \) is for assigning a value (putting it in the box). A double \( == \) is for comparing two values (checking if they are the same). This is a very frequent error in exams!

4. Working with Different Languages

In your Paper 2 exam, you will use either Java or Python. While the logic is the same, the "grammar" (syntax) looks slightly different.

In Python:
Python is "dynamically typed," meaning you don't have to tell the computer what data type you are using. It figures it out for you.
Example: \( age = 17 \)

In Java:
Java is "strongly typed," meaning you must declare the data type before the variable name.
Example: \( int \text{ } age = 17; \)

Did you know? Python was named after the comedy group Monty Python, not the snake! Java was named after the coffee the creators drank in large quantities.

Chapter Summary Checklist

- Can you define what a variable is?
- Do you know the difference between an Integer and a Real/Float?
- Can you explain what the Modulus (%) operator does?
- Do you know when to use \( == \) versus \( = \)?
- Are you comfortable using AND, OR, and NOT to combine logic?

Next Step: Now that you know how to store data and do basic math, you're ready to learn how programs make decisions in the next chapter: Selection and Iteration!