☕ Java
Control Flow Statements in Java
Without control flow, every program would just run from top to bottom in a straight line. Control flow lets your program make decisions, repeat actions, and skip code — the difference between a calculator and an actual application.
if-else — Making Decisions
The if-else block is how your program makes choices. It evaluates a condition (true/false) and runs the matching block. Real apps use this constantly — checking login status, validating inputs, routing requests.
Java
int marks = 75;
if (marks >= 90) {
System.out.println("Grade: A — Outstanding!");
} else if (marks >= 75) {
System.out.println("Grade: B — Good job");
} else if (marks >= 60) {
System.out.println("Grade: C — Passing");
} else {
System.out.println("Grade: F — Try again");
}
// Output: Grade: B — Good jobswitch — Cleaner Multi-Branch Logic
When you're checking one variable against many possible values, switch is cleaner than a chain of if-else if. Think of it as a routing table — "if the value is X, do this; if it's Y, do that."
Java
String day = "Monday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
System.out.println("Weekday — back to work");
break;
case "Saturday":
case "Sunday":
System.out.println("Weekend — relax!");
break;
default:
System.out.println("Unknown day");
}
// Modern Switch Expression (Java 14+) — much cleaner
String type = switch (day) {
case "Saturday", "Sunday" -> "Weekend";
default -> "Weekday";
};for Loop — When You Know the Count
Use a for loop when you know exactly how many times you want to repeat something — printing 10 rows, processing 100 records, iterating over an array.
Java
// Classic for loop
for (int i = 1; i <= 5; i++) {
System.out.println("Attempt: " + i);
}
// Enhanced for loop (for-each) — cleaner when iterating collections
int[] scores = {85, 92, 78, 95, 88};
for (int score : scores) {
System.out.println("Score: " + score);
}
// You can't modify the original array with for-each though — use classic for thatwhile and do-while — When You Don't Know the Count
Use while when you want to keep looping as long as some condition is true. Classic examples: reading user input until they type "quit", retrying a network request until it succeeds, polling a sensor until a threshold is hit.
do-while is the same, but it always runs at least once — useful when you need to execute before checking the condition (like showing a menu once before asking "run again?").
Java
// while — checks condition BEFORE each iteration
int attempts = 0;
while (attempts < 3) {
System.out.println("Trying to connect... attempt " + (attempts + 1));
attempts++;
}
// do-while — runs FIRST, then checks condition
// Good for: "show menu, then ask if they want to continue"
String input;
do {
System.out.println("Enter a positive number:");
input = scanner.nextLine();
} while (Integer.parseInt(input) <= 0);Related Topics in Java Basics
Variables in Java
A variable is just a named box in memory that holds a value. Java is strict about what goes in each box — you tell it the type upfront. Once you get this, the rest of Java clicks into place.
Data Types in Java
Java needs to know exactly what kind of data it's dealing with before it can store or process it. Integers, decimals, characters, true/false — each has its own type. Knowing which to use (and why) makes your programs efficient and bug-free.
Operators in Java
Operators are the symbols that tell Java what to do with your data — add it, compare it, flip it, combine it. You use operators in literally every line of real code, so getting comfortable with them early pays off immediately.
Arrays in Java
An array is how you store a list of values under one name. Instead of creating 100 separate variables for 100 exam scores, you create one array. Simple idea, but it unlocks a ton of power.