Mastering Java Control Flow Without Expensive Courses
Sequential Execution
The simplest program structure exeuctes statements one after another.
package demo.flow;
public class SequentialDemo {
public static void main(String[] args) {
int counter = 10;
System.out.println("Initial value: " + counter);
counter += 5;
System.out.println("After increment: " + counter);
if (counter % 2 == 0) {
System.out.println("Even result");
} else {
System.out.println("Odd result");
}
}
}
Single-Branch Decision
Use if when only one outcome matters.
package demo.flow;
import java.util.Scanner;
import java.util.regex.Pattern;
public class SingleBranchCheck {
private static final Pattern INT_PATTERN = Pattern.compile("^-?\\d+$");
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
System.out.print("Type something: ");
String token = in.nextLine().trim();
if (INT_PATTERN.matcher(token).matches()) {
int value = Integer.parseInt(token);
System.out.println("Parsed integer: " + value);
}
}
}
}
Dual-Branch Decision
if-else handles exactly two mutually exclusvie paths.
package demo.flow;
import java.util.Scanner;
public class DualBranchClassifier {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter text: ");
String line = sc.nextLine();
if (line.matches("\\d+")) {
System.out.println("Digits detected");
} else if (line.matches("[A-Za-z]+") && line.length() > 0) {
System.out.println("Letters detected");
} else {
System.out.println("Mixed or empty input");
}
}
}
Nested Conditions
Combine decisions when finer granularity is needed.
package demo.flow;
import java.util.Scanner;
public class NestedAnalyzer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input: ");
String raw = input.nextLine();
if (raw.matches("\\d+")) {
int num = Integer.parseInt(raw);
if (num % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
} else if (raw.matches("[A-Za-z]+") && raw.length() > 0) {
System.out.println("Upper: " + raw.toUpperCase());
System.out.println("Lower: " + raw.toLowerCase());
} else {
String reversed = new StringBuilder(raw).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
}
Mini Bank Simulatino
A concise example that ties variables, I/O, and branching together.
package demo.flow;
import java.util.Scanner;
public class MiniBank {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String owner = sc.nextLine();
System.out.print("Initial balance: ");
double balance = sc.nextDouble();
System.out.print("Amount to transact: ");
double amount = sc.nextDouble();
System.out.print("1 = deposit, 2 = withdraw: ");
int choice = sc.nextInt();
if (choice == 1) {
balance += amount;
System.out.println("Deposited. New balance: " + balance);
} else if (choice == 2) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn. New balance: " + balance);
} else {
System.out.println("Insufficient funds");
}
} else {
System.out.println("Invalid option");
}
}
}