πŸ”„ Control Flow Statements

Master decision-making and repetition in Java with visual flowcharts and real-world applications


🎯 Learning Objectives

By the end of this lesson, you will:


🧠 Control Flow Architecture

<aside> 🎭

Program Flow Philosophy

Control flow is the heartbeat of programming - it determines how your program makes decisions, repeats actions, and responds to different conditions. Think of it as the GPS of your code!

</aside>

The Decision-Making Hierarchy

Program Execution Flow
═══════════════════════════════════════════════════════════════

Sequential Flow:      Decision Flow:        Repetitive Flow:
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ Step 1  β”‚          β”‚Conditionβ”‚           β”‚  Loop   β”‚
   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
        β”‚                   β•±β”‚β•²                    β”‚ ↑
   β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”          β•±   β”‚  β•²                   β”‚ β”‚
   β”‚ Step 2  β”‚       Yesβ•±    β”‚   β•²No               β”‚ β”‚
   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜        β•±      β”‚    β•²                β”‚ β”‚
        β”‚       β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”    β”‚   β”Œβ”€β–Όβ”€β”€β”€β”           β”‚ β”‚
   β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”  β”‚Action1β”‚    β”‚   β”‚Actionβ”‚           β””β”€β”˜
   β”‚ Step 3  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚   β”‚  2  β”‚        Repeat
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚   β””β”€β”€β”€β”€β”€β”˜       Until Done

🚦 Conditional Statements Mastery

if-else Decision Tree

<aside> 🌳

Decision Tree Visualization

Think of if-else as a flowchart where each condition is a branch point leading to different actions.

</aside>

// 🎯 Practical Example: Student Grade Calculator
public class GradeCalculator {
    
    public static String calculateGrade(int score, boolean hasExtraCredit) {
        String grade;
        String performance;
        
        // Multi-level decision making
        if (score >= 97 || (score >= 95 && hasExtraCredit)) {
            grade = "A+";
            performance = "Outstanding! 🌟";
        } else if (score >= 93) {
            grade = "A";
            performance = "Excellent work! 🎯";
        } else if (score >= 90) {
            grade = "A-";
            performance = "Great job! πŸ‘";
        } else if (score >= 87) {
            grade = "B+";
            performance = "Good work! πŸ“ˆ";
        } else if (score >= 83) {
            grade = "B";
            performance = "Solid effort! πŸ’ͺ";
        } else if (score >= 80) {
            grade = "B-";
            performance = "Keep improving! πŸ“š";
        } else if (score >= 77) {
            grade = "C+";
            performance = "Need more practice! πŸ”„";
        } else if (score >= 73) {
            grade = "C";
            performance = "Study harder! πŸ“–";
        } else if (score >= 70) {
            grade = "C-";
            performance = "Focus on basics! 🎯";
        } else if (score >= 60) {
            grade = "D";
            performance = "Needs significant improvement! ⚠️";
        } else {
            grade = "F";
            performance = "Must retake course! πŸ”„";
        }
        
        return String.format("Grade: %s - %s (Score: %d)", grade, performance, score);
    }
}

Visual Decision Flow Chart