1. What is Control Flow?
Control flow refers to the order in which statements are executed in a program.
Normally, Java executes statements one by one, from top to bottom. This is called sequential execution.
Example:
public class Main {
public static void main(String[] args) {
System.out.println("Start");
System.out.println("Learning Java");
System.out.println("End");
}
}
Output:
Start
Learning Java
End
But in real-world applications, we sometimes need to make decisions based on conditions. For example, an ATM should allow a withdrawal only when the account has sufficient balance.
This is where control flow statements are useful.
2. Types of Control Flow Statements
Java control flow statements are commonly divided into three categories:
- Decision-making statements
- Looping statements
- Jump statements
Decision-making statements
- if
- if-else
- else-if
- switch
Looping statements
- for
- while
- do-while
Jump statements
- break
- continue
- return
Decision-Making Statements
1. if Statement
The if statement executes a block of code only when the given condition is true.
Syntax:
if (condition) {
// Code executes if condition is true
}
Example:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote");
}
}
}
Output:
Eligible to vote
Here, the condition age >= 18 is true, so Java executes the code inside the if block.
If the condition is false, Java skips the if block.
2. if-else Statement
The if-else statement is used when we want to execute one block of code if the condition is true and another block if the condition is false.
Syntax:
if (condition) {
// Executes when condition is true
} else {
// Executes when condition is false
}
Example:
public class Main {
public static void main(String[] args) {
int balance = 2000;
int withdrawAmount = 5000;
if (balance >= withdrawAmount) {
System.out.println("Withdrawal successful");
} else {
System.out.println("Insufficient balance");
}
}
}
Output:
Insufficient balance
Here, the account balance is less than the withdrawal amount. Therefore, the else block executes.
3. else-if Ladder
An else-if ladder is used when we have multiple conditions to check.
For example, in an examination result system, we may assign different grades based on students' marks.
Example:
public class Main {
public static void main(String[] args) {
int marks = 82;
if (marks < 0 || marks > 100) {
System.out.println("Invalid marks");
} else if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
}
}
Output:
Grade B
Here, Java checks the conditions from top to bottom. When it finds the first true condition, it executes that block and skips the remaining conditions.
The first condition validates the marks to ensure they are between 0 and 100.
4. switch Statement
A switch statement is another decision-making statement. It is useful when we need to execute different blocks of code based on the value of an expression.
For example, an ATM application can display different operations based on the customer's selection.
Syntax:
switch (expression) {
case value1:
// Code
break;
case value2:
// Code
break;
default:
// Default code
}
Example:
public class Main {
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
System.out.println("Check Balance");
break;
case 2:
System.out.println("Withdraw Money");
break;
case 3:
System.out.println("Deposit Money");
break;
case 4:
System.out.println("Exit");
break;
default:
System.out.println("Invalid Choice");
}
}
}
Output:
Withdraw Money
Here, the value of choice is 2, so Java executes case 2.
What is break?
The break statement terminates the switch statement. After executing the matching case, Java exits the switch when it encounters break.
What is default?
The default block executes when none of the case values match the switch expression.
What is Fall-through?
If we remove break from a case, Java continues executing the statements in the following cases until it encounters a break or reaches the end of the switch.
Example:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid Day");
}
Output:
Tuesday
Wednesday
Here, case 2 does not contain a break. Therefore, Java continues executing case 3. This behaviour is called fall-through.
Difference Between if-else and switch
| if-else | switch |
|---|---|
| Checks conditions or expressions. | Matches an expression against case values. |
| Supports complex Boolean conditions. | Uses matching case labels. |
| Useful for ranges and multiple conditions. | Useful for fixed choices and menu options. |
Uses else as the final alternative. |
Uses default as the fallback. |
Conclusion
Control flow statements are essential in Java because they help programs make decisions based on different conditions.
-
ifexecutes code when a condition is true. -
if-elsechooses between two blocks. -
else-ifchecks multiple conditions. -
switchselects a matching case. -
breakexits a switch statement. -
defaulthandles unmatched switch values.
Understanding these statements provides a strong foundation for developing real-world Java applications such as ATM systems, employee management systems, banking applications, and student result management systems.
Top comments (0)