Today I explored one of the most important concepts in Java β Control Flow Statements.
Control flow statements help us decide:
π Which code should execute
π When it should execute
π How many times it should execute
These are very important when building real-world applications and automation scripts.
πΉ What are Control Flow Statements?
Control flow statements control the execution flow of a program.
**
There are 3 types:**
1οΈβ£ Decision Making Statements
2οΈβ£ Looping Statements
3οΈβ£ Jump Statements
π 1οΈβ£ Decision Making Statements
Used to take decisions based on conditions.
β if
Executes code only if condition is true
β else-if
Used to check multiple conditions
β else
Executes when all conditions fail
β switch
Used for multiple fixed values
π 2οΈβ£ Looping Statements
β while loop
β for loop
β do-while loop
π 3οΈβ£ Jump Statements
| Statement | Purpose |
| ---------- | --------------- |
| `break` | Stops loop |
| `continue` | Skips iteration |
π‘ Scenario-Based Questions (if / else-if / else)
π¦ Scenario 1: ATM Withdrawal System
β Question
A user tries to withdraw money from an ATM.
If withdrawal amount is less than or equal to balance β show "Successfully debited"
Else β show "Insufficient balance"
private void scenario1() {
int balance = 1000;
int userWA = 200;
if (userWA <= balance) {
System.out.println("Successfully Debited " + userWA);
} else {
System.out.println("Insufficient Balance");
}
}
π Explanation
β Condition checks whether user has enough balance
β Executes appropriate block based on result
π€ Output
Successfully Debited 200
π Scenario 2: Online Food Delivery Status
β Question
An order goes through multiple stages:
If "Placed" β show Waiting for confirmation
If "Preparing" β show Food is being prepared
If "Out for delivery" β show Delivery partner on the way
Else β show Delivered
private void scenario2(String userState) {
if (userState.equalsIgnoreCase("placed")) {
System.out.println("Waiting for restaurant confirmation");
} else if (userState.equalsIgnoreCase("preparing")) {
System.out.println("Food is being prepared");
} else if (userState.equalsIgnoreCase("out for delivery")) {
System.out.println("Delivery partner on the way");
} else {
System.out.println("Delivered");
}
}
π Explanation
β Uses else-if ladder for multiple conditions
β equalsIgnoreCase() handles case sensitivity
π Scenario 3: Exam Result System
β Question
A studentβs result is based on marks:
< 35 β Fail
35β60 β Pass
60β80 β First Class
80 β Distinction
private void scenario3() {
int mark = 81;
if (mark < 35)
System.out.println("Fail");
else if (mark >= 35 && mark <= 60)
System.out.println("Pass");
else if (mark > 60 && mark <= 80)
System.out.println("First Class");
else
System.out.println("Distinction");
}
π Explanation
β Uses range conditions with && operator
β Ensures only one block executes
π€ Output
Distinction
π¦ Scenario 4: Traffic Signal Control
β Question
A driver reacts based on signal color:
Red β Stop
Yellow β Get ready
Green β Go
Else β Invalid signal
private void scenario4(String signalcolor) {
if (signalcolor.equalsIgnoreCase("red"))
System.out.println("Stop the Vehicle");
else if (signalcolor.equalsIgnoreCase("yellow"))
System.out.println("Get Ready");
else if (signalcolor.equalsIgnoreCase("green"))
System.out.println("Go");
else
System.out.println("Invalid Signal");
}
π Explanation
β Uses string comparison
β Handles different input cases using equalsIgnoreCase()
π Scenario 5: Login Authentication System
β Question
A user tries to log in:
If username is wrong β "Invalid Username"
Else if password is wrong β "Invalid Password"
Else if account is locked β "Account Locked"
Else β Allow login
static String username = "bala";
static String password = "bala1234";
static boolean accountLocked = true;
public class Decisionmaking {
static String username = "bala";
static String password = "bala1234";
static boolean accountLocked = true;
public static void main(String[] args) {
Decisionmaking demo = new Decisionmaking();
demo.sceanrio5();
private void scenario5() {
if (!username.equals("bala"))
System.out.println("Invalid Username");
else if (!password.equals("bala1234"))
System.out.println("Invalid Password");
else if (accountLocked)
System.out.println("Account is Locked");
else
System.out.println("Allow login");
}
π Explanation
β ! operatoris used for negation
β Conditions are evaluated in sequence
β Once matched β remaining conditions are skipped
π€ Output
Account is Locked
π§ Key Takeaways
β Control flow defines how program executes
β if-else handles decision making
β else-if handles multiple conditions
β Loops reduce repetitive code
β break & continue control loops
π Conclusion
Todayβs learning helped me understand how Java programs make decisions and control execution flow.
These concepts are extremely important in automation testing, where we need to:
β Handle different test conditions
β Validate application behavior
β Write dynamic scripts
Step by step, Iβm building a strong foundation in Java for Selenium Automation π
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)