Welcome to the Control Center! Selection and Iteration

Welcome! So far, you have learned how to store data and perform basic math. But right now, your code is like a train on a single track—it can only go straight forward. In this unit, we are going to give your programs a "brain" and "stamina."

Selection (Decision Making) allows your code to make choices based on different conditions. Iteration (Looping) allows your code to repeat tasks over and over without you having to write the same line a hundred times. These two concepts are the "secret sauce" that makes software powerful!

Part 1: Boolean Expressions (The Yes/No Questions)

Before your computer can make a choice, it needs to evaluate a Boolean expression. This is just a fancy way of saying a question that results in either true or false.

Relational Operators

We use these symbols to compare values:

== : Equal to (Note: Use two equals signs! One equals sign is for assigning values, two is for comparing them.)
!= : Not equal to
< and > : Less than and Greater than
<= and >= : Less than or equal to and Greater than or equal to

Logical Operators

Sometimes one condition isn't enough. We can combine them using logical operators:

1. ! (NOT): Flips the result. \( !(true) \) becomes false.
2. && (AND): Both sides must be true for the whole thing to be true. \( (5 > 3 \text{ && } 2 < 4) \) is true.
3. || (OR): At least one side must be true. \( (5 < 3 \text{ || } 2 < 4) \) is true because the second part is true.

Quick Review: Think of && like a strict parent (you must clean your room AND do your homework to go out). Think of || like a relaxed friend (we can watch a movie OR play games).

Key Takeaway:

Every decision in Java boils down to a single true or false value.

Part 2: Selection (If-Else Statements)

Now that we can ask questions, we can use if statements to act on the answers.

The One-Way Selection

If the condition is true, the code inside the curly braces runs. If it's false, Java just skips it.
Example: if (isRaining) { takeUmbrella(); }

The Two-Way Selection (If-Else)

This provides an "otherwise" option. If the "if" part is false, the "else" part must run.
Example: if (score >= 60) { pass(); } else { fail(); }

The Multi-Way Selection (Else If)

When you have many possibilities, use else if. Java checks them in order from top to bottom and stops as soon as it finds a true one.
Example: If it's 90 degrees, stay inside. Else if it's 70, go for a walk. Else, wear a jacket.

Common Mistake: Don't put a semicolon after your if-statement condition! Writing if (x == 5); will break your logic because the computer thinks the "if" statement ends right there.

Key Takeaway:

Selection allows your program to follow different paths depending on the data it receives.

Part 3: Comparing Objects (The String Trap)

Don't worry if this seems tricky at first—almost everyone makes this mistake! In Java, you should not use == to compare Strings. Using == checks if the two variables point to the exact same "box" in memory, not if the text inside the boxes is the same.

To compare text, always use the .equals() method:
if (name.equals("Java")) { ... }

Part 4: Iteration (Loops)

Iteration is just a fancy word for repeating something. There are two main types of loops you need to know for the AP Exam.

The While Loop

A while loop repeats as long as its condition is true. It's like saying, "While I am still hungry, I will keep eating."

Step-by-Step:
1. Check the condition.
2. If true, run the code inside.
3. Go back to Step 1.
4. If false, exit the loop.

Danger! If your condition never becomes false, you get an Infinite Loop, and your program will crash or freeze. Always make sure something inside the loop changes so it can eventually end!

The For Loop

A for loop is usually used when you know exactly how many times you want to repeat something. It has three parts:
for (initialization; condition; increment)

Example: for (int i = 0; i < 5; i++) { System.out.println("Hello!"); }
This loop starts i at 0, checks if i is less than 5, runs the code, and then adds 1 to i. It repeats this 5 times.

Did you know? Programmer's usually start counting at 0 instead of 1. It takes a little getting used to, but you'll be a pro in no time!

Key Takeaway:

Use a while loop when you don't know the number of repetitions. Use a for loop when you do.

Part 5: Logic Laws and Nested Loops

De Morgan's Law

Sometimes we have complex "NOT" logic. De Morgan's Law helps us simplify it. To "distribute" a NOT sign (!):
1. Flip the && to || (or vice-versa).
2. Apply the ! to both individual terms.

Example: \( !(A \text{ && } B) \) is the same as \( (!A \text{ || } !B) \)

Nested Loops

This is a loop inside another loop. Think of it like a clock: the inner loop (the seconds hand) must finish a full rotation before the outer loop (the minutes hand) moves forward just one tick.

The "Off-By-One" Error:
This is the most common loop mistake. It happens when your loop runs one time too many or one time too few. Usually, this is because you used < when you should have used <=. Always trace the first and last numbers of your loop to be sure!

Key Takeaway:

Logic can be simplified using De Morgan's Law, and nested loops are perfect for working with grids or complex patterns.

Summary Checklist

• Can you identify the difference between == and .equals()?
• Do you know when to use a while loop versus a for loop?
• Can you predict how many times a loop will run?
• Can you use &&, ||, and ! to build complex logic?

If you can answer "yes" to these, you are well on your way to mastering Unit 2! Keep practicing—coding is a muscle that gets stronger every time you use it.