Crash Course — Unit 2: Selection and Iteration
In simple terms: Hey there, let's talk about Unit 2. This unit is the heart of programming logic—it’s all about making decisions and repeating actions. You'll learn how to use `if` statements to make your code choose a path, and `for` and `while` loops to make it perform tasks over and over. Mastering this is crucial because it’s the foundation for building any interesting or complex program, and it's a huge part of the AP exam.
Crash Course — Unit 2: Selection and Iteration
1 / 4
- Boolean Expressions — The
trueorfalsequestions that guide your program's decisions, likescore > 90. - Relational Operators — The tools (
==,!=,<,>,<=,>=) you use to build Boolean expressions by comparing values. if-else if-elseStatements — The fundamental structure for making your code choose one path out of many possible options.- Compound Boolean Expressions (
&&,||,!) — Combining simple questions with AND, OR, and NOT to ask more complex ones, likeisRaining && isCold. - Short-Circuit Evaluation — Java's clever way of saving time; it stops evaluating a compound expression (
&&or||) as soon as it knows the final answer. - De Morgan's Laws — A specific way to rewrite and simplify compound Boolean expressions, often tested on the multiple-choice section.
- Object Comparison (
==vs..equals()) — A critical distinction.==checks if two variables point to the exact same object in memory, while.equals()checks if their contents are meaningfully the same.
whileLoops — Repeats a block of code as long as a condition remains true. Use when you don't know the exact number of repetitions ahead of time.forLoops — Repeats a block of code a specific number of times. The go-to loop for iterating over a known range or a String's length.- String Traversal — Using a
forloop to visit and inspect every single character in a String, one by one. - Nested Loops — Placing one loop inside another. This is your tool for working with 2D data, like a grid of seats in a stadium or pixels on a screen.
- Statement Execution Count — A simple way to analyze an algorithm's efficiency by counting how many times a key statement runs as the input gets larger.
Key Formulas / Terms
if-else if-elseStructure:if (condition1) { // Do this } else if (condition2) { // Do this instead } else { // Do this if nothing above was true }- De Morgan's Laws:
!(A && B)is equivalent to!A || !B!(A || B)is equivalent to!A && !B
- Standard
forLoop (for counting):for (int i = 0; i < count; i++) { // This code runs 'count' times } - Standard
whileLoop (until a condition is met):int ticketsSold = 0; while (ticketsSold < 100) { // Sell a ticket... ticketsSold++; // CRITICAL: Update the variable! } - String Traversal Loop:
String message = "Hello"; for (int i = 0; i < message.length(); i++) { System.out.println(message.charAt(i)); } - Object vs. Primitive Comparison:
int a = 5; int b = 5;->a == bistrue.String s1 = new String("hi"); String s2 = new String("hi");->s1 == s2isfalse, buts1.equals(s2)istrue.
Exam Traps
- Trap
=vs.==. Using a single equals sign (=) for assignment inside anifstatement's condition, when you meant to use the double equals (==) for comparison. · Counter: Always double-check yourifconditions. Your compiler will often catch this, but on paper, it's an easy mistake to make and a common wrong answer choice. - TrapOff-by-One Errors. Your loop runs one too many or one too few times. This usually happens when you mix up
<and<=, or start your loop index at1instead of0. · Counter: For a String oflength(), the valid indices are0tolength() - 1. Your loop condition should almost always bei < str.length(). - TrapComparing Objects with
==. The exam will show you twoStringor other objects created withnewand ask ifobj1 == obj2is true. It's almost always false. · Counter: Internalize this now:==checks for the same memory location (are they the exact same object?)..equals()checks for the same content (do they look the same?). For Strings, always use.equals()to compare their values. - TrapDangling
else. In a nestedifstructure, anelsestatement can look like it belongs to the outerif, but it doesn't. · Counter: Remember the rule: anelsealways pairs with the nearest, precedingifthat isn't already paired with anelse. Use curly braces{}to make your intention clear, even when they aren't strictly required. - TrapInfinite
whileLoops. You write awhileloop but forget to include the code inside the loop that will eventually make the condition false. · Counter: Before you finish writing awhileloop, ask yourself: "What line of code inside this loop is making progress toward the exit?" Make sure you update the variable that's being checked in the condition.
Quiz me — 17 cards
Tap a card to reveal the answer. Use this to self-test before the exam.
Boolean Expressions
Boolean Expressions — what's the key idea?
Boolean Expressions
�� The
true or false questions that guide your program's decisions, like score > 90.1 / 17