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:
- Master
if-elseandswitchbranching. - Use
for,while, anddo-whileloops. - Learn to use
breakandcontinuekeywords.
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
if (condition) {
// True block
} else {
// False block
}
Part 3 — Example
Problem: Determine if a number is positive or negative.
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
for (int i=0; i<5; i++) { ... }
while (condition) { ... }
Part 3 — Example
Problem: Print the first 5 even numbers.
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
- Take a number as input.
- Print its table from 1 to 10.
- Skip the 5th iteration using the
continuekeyword.
Step-by-Step Process
- Use a
Scannerfor input. - Write a
forloop from 1 to 10. - Inside, use an
ifto check ifi == 5and callcontinue. - Print the result.
Expected Deliverables
TableGenerator.javasource code.