Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Mastering Java Control Flow Without Expensive Courses

Tech May 8 3

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");
        }
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.