Unit Big Picture
This unit moves beyond simple, sequential programs. You will learn to control the flow of execution by making decisions and repeating actions. We will start with the logic of true and false, build conditional statements to create branching paths in your code, and then master loops to perform repetitive tasks efficiently. By the end of this unit, you will be able to write sophisticated algorithms that solve problems by intelligently managing program flow.
Core Threads
Thread 1: Control Flow
What it is: Directing the path of execution in a program using conditional logic and repetition. Instead of running line-by-line, the program can now make choices (
if) or repeat sections of code (while,for).Why it matters: Control flow is what makes software dynamic and intelligent. It allows a program to react to different inputs, handle various conditions, and process large amounts of data without redundant code.
Thread 2: Algorithm Design
What it is: Developing a step-by-step, repeatable process to solve a problem. This unit provides the core building blocks for translating logical steps—like "check if a number is even" or "find the first vowel in a word"—into functional Java code.
Why it matters: Nearly all programming is a form of algorithm design. Mastering selection and iteration enables you to move from simply writing code to architecting effective and efficient solutions to complex problems.
Key Java Concepts & Data Structures
| Java Concept / Data Structure | What It Is (1-Sentence) | Why It Matters (1-Sentence) |
|---|---|---|
boolean | A primitive data type that can only hold the value true or false. | It is the foundation of all decision-making in Java. |
| Relational Operators | Symbols (>, <, ==, !=) that compare two values and produce a boolean result. | They allow you to form the logical questions that control your program's flow. |
| Logical Operators | Operators (&&, ` | |
if-else Statement | A control structure that executes a block of code if a condition is true, and an optional other block if it is false. | This is the primary tool for creating branching paths and making decisions in code. |
while Loop | A control structure that repeatedly executes a block of code as long as its boolean condition remains true. | It is ideal for situations where the number of repetitions is not known beforehand. |
for Loop | A control structure that combines initialization, a condition, and an update step to execute a block of code a specific number of times. | It is perfect for definite, counter-controlled iteration, such as processing every character in a String. |
Unit Concept & Syntax Bank
booleanexpression: Any expression that evaluates to eithertrueorfalse. It is the required condition for all selection and iteration statements.Relational Operators: The set of operators for comparison:
==(is equal to),!=(is not equal to),>(greater than),<(less than),>=(greater than or equal to), and<=(less than or equal to).Logical Operators:
&&(logical AND),||(logical OR), and!(logical NOT). These are used to create compound boolean expressions.Short-Circuit Evaluation: The behavior of
&&and||where the second operand is only evaluated if necessary. Fora && b,bis not evaluated ifaisfalse.De Morgan's Laws: A set of rules for transforming logical expressions. For example,
!(a && b)is equivalent to!a || !b.if-else if-else: A chain of conditional statements that checks conditions sequentially and executes only the first block whose condition istrue.int score = 85; if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); // This block runs } else { System.out.println("C or lower"); }whileLoop: A pre-test loop that checks its condition before each iteration. The loop body might never execute.int i = 0; while (i < 3) { System.out.println(i); // Prints 0, 1, 2 i++; }forLoop: A compact loop structure that is ideal when you know the number of iterations.for (int i = 0; i < 3; i++) { System.out.println(i); // Prints 0, 1, 2 }.equals()vs.==: The.equals()method compares the contents of twoStringobjects, while==checks if twoStringreferences point to the exact same object in memory. Always use.equals()forStringcontent comparison.
Topic Navigator
| Topic Title | What This Adds (<=10 words) |
|---|---|
| 2.1: Algorithms with Selection and Repetition | Introducing the concepts of making choices and repeating actions. |
| 2.2: Boolean Expressions | Learning the true/false logic that powers decisions. |
| 2.3: if Statements | Executing code only when a specific condition is met. |
| 2.4: Nested if Statements | Making decisions within other decisions for complex logic. |
| 2.5: Compound Boolean Expressions | Combining multiple logical checks into a single condition. |
| 2.6: Comparing Boolean Expressions | Understanding logical equivalency, like with De Morgan's Laws. |
| 2.7: while Loops | Repeating code as long as a condition stays true. |
| 2.8: for Loops | Repeating code a specific, predetermined number of times. |
| 2.9: Implementing Selection and Iteration | Applying these structures to solve standard algorithmic problems. |
| 2.10: Implementing String Algorithms | Using loops and conditionals to process and analyze Strings. |
| 2.11: Nested Iteration | Placing loops inside other loops to solve complex problems. |
| 2.12: Informal Run-Time Analysis | Beginning to analyze and compare the efficiency of algorithms. |
Exam Skills Focus
Code Implementation: Writing methods that use
if,while, andforstatements to correctly implement a given algorithm.Code Analysis: Tracing the execution of code containing loops and conditionals to determine the final output or return value.
Application & Design: Choosing the most appropriate control structure (
if,while, orfor) to solve a particular problem efficiently.
Common Misconceptions & Clarifications
Using
==to compareStrings: The==operator checks if twoStringvariables refer to the same object in memory, not if they contain the same characters. Always use the.equals()method for comparingStringcontent.Off-by-One Errors in Loops: A loop that runs one too many or one too few times is a common bug. Pay close attention to your loop's starting value, its boolean condition (
<vs.<=), and its update step.Infinite Loops: A
whileloop whose condition never becomesfalsewill run forever. Ensure that some action inside the loop's body will eventually cause the condition to be falsified.
Summary
This unit provides the fundamental tools for algorithmic thinking in Java. You began with boolean logic, the simple true/false foundation for all program decisions. From there, you learned to build conditional paths using if-else statements and to create efficient, repetitive processes with while and for loops. By combining and nesting these structures, you can now write powerful algorithms to process data, solve problems, and control program behavior. Mastering control flow is the critical step toward creating dynamic and useful software.