Java Basics Tutorial: Mastering Control Flow Statements
Welcome to your comprehensive guide to Java control flow statements! We'll break down each concept with clear examples and detailed explanations to ensure you grasp these fundamental programming constructs.
1. If-then/if-then-else Statement
int age = 18;
if (age >= 18) {
System.out.println("You are an adult!");
} else {
System.out.println("You are a minor!");
}
How it works:
-
We declare an integer variable
age
and initialize it with value 18 - The
if
condition checks if age is 18 or older - If true (which it is in this case), executes the first code block
- If false, would execute the
else
block -
Curly braces
{}
define the code blocks for each condition
Output: "You are an adult!"
2. Switch-case Statement
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}
Key points:
- Evaluates the
day
variable against multiple cases break
prevents "falling through" to next casesdefault
case handles unexpected values- Works with: byte, short, char, int, enums, and Strings
Output: "Wednesday"
3. For Loop
for(int i = 0; i < 5; i++) {
System.out.println("Count: " + i);
}
Structure breakdown:
-
Initialization:
int i = 0
(runs once at start) -
Condition:
i < 5
(checked before each iteration) -
Increment:
i++
(executes after each iteration)
Execution Flow:
0 → 1 → 2 → 3 → 4 (stops when i reaches 5)
4. Do-while Loop
int count = 5;
do {
System.out.println("Count: " + count);
count--;
} while(count > 0);
Special feature:
- Guarantees at least one execution
- Condition check happens after loop body
- Useful for menu systems or input validation
Output sequence: 5, 4, 3, 2, 1
5. Break, Continue & Return
// Break example
for(int i = 0; i < 10; i++) {
if(i == 5) break;
System.out.println(i);
}
// Continue example
for(int i = 0; i < 5; i++) {
if(i == 2) continue;
System.out.println(i);
}
// Return example
public int add(int a, int b) {
return a + b;
}
Break: Exits the loop immediately
Continue: Skips current iteration, continues next
Return: Exits method and optionally returns value
Outputs:
Break: 0-4
Continue: 0,1,3,4
Return: Returns sum of two numbers
6. Static & Final Keywords
// Static example
class Counter {
static int count = 0;
Counter() { count++; }
}
// Final example
final double PI = 3.14159;
Static:
- Belongs to class rather than instances
- All objects share the same static variable
- Creating 3 Counter objects makes count = 3
Final:
- Makes variable constant/unchangeable
- Attempting to modify PI causes error
- Naming convention: ALL_CAPS
7. Nested Loops
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
System.out.print(i*j + " ");
}
System.out.println();
}
Pattern explanation:
- Outer loop runs 3 times (i=1,2,3)
- Inner loop completes full cycle for each outer iteration
- Prints multiplication table rows
Output:
1 2 3
2 4 6
3 6 9
Conclusion
You've now explored Java's core control flow mechanisms. Remember:
- Conditionals (if/switch) make decisions
- Loops (for/while) handle repetition
- Keywords modify flow behavior
Practice creating different combinations and observe the results. Try modifying these examples - change values, remove breaks, or nest different loop types!