Chapter 3 — Java Control Statements

📚 Chapter Overview

Intelligence in programming comes from decision-making and repetition. In this chapter, you will learn how to use if-else blocks to make choices and loops to repeat tasks efficiently.

Learning Objectives:

3.1 Decision Making (if-else)

Part 1 — Definition

Conditional statements allow a program to execute different blocks of code based on a condition. The if statement checks a boolean condition. If true, it runs the code; otherwise, it jumps to the else block.

Part 2 — Syntax

Main.java
if (condition) {
    // True block
} else {
    // False block
}

Part 3 — Example

Problem: Determine if a number is positive or negative.

Main.java
int num = -5;
if (num > 0) {
    System.out.println("Positive");
} else {
    System.out.println("Negative");
}

Part 4 — Video

Video: Coming Soon


3.2 Looping (for & while)

Part 1 — Definition

Loops are used to execute a block of code multiple times. A for loop is used when you know the number of iterations. A while loop is used when the number of iterations depends on a condition.

Part 2 — Syntax

Main.java
for (int i=0; i<5; i++) { ... }
while (condition) { ... }

Part 3 — Example

Problem: Print the first 5 even numbers.

EvenNumbers.java
for (int i = 1; i <= 5; i++) {
    System.out.println(i * 2);
}

Output: 2, 4, 6, 8, 10

Part 4 — Video

Video: Coming Soon


🏆 Chapter Challenge

Challenge Objective

Build a "Multiplication Table Generator" that uses both loops and conditionals.

Requirements

Step-by-Step Process

  1. Use a Scanner for input.
  2. Write a for loop from 1 to 10.
  3. Inside, use an if to check if i == 5 and call continue.
  4. Print the result.

Expected Deliverables

Solve in Compiler