Master decision-making and repetition in Java with visual flowcharts and real-world applications
By the end of this lesson, you will:
<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>
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
<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);
}
}