The if statement executes a block of code only when a condition is true. In test automation, every decision — retry on failure, skip on condition, validate output — uses if/else.
public class IfBasics {
public static void main(String[] args) {
int statusCode = 200;
if (statusCode == 200) {
System.out.println("Request successful");
}
if (statusCode == 404) {
System.out.println("Page not found");
}
}
}The else block runs when the condition is false — it handles the alternative case.
public class IfElse {
public static void main(String[] args) {
String pageTitle = "Dashboard";
String expected = "Dashboard";
if (pageTitle.equals(expected)) {
System.out.println("PASS: Title matches");
} else {
System.out.println("FAIL: Expected \"" + expected + "\" but got \"" + pageTitle + "\"");
}
}
}public class ElseIf {
public static void main(String[] args) {
int statusCode = 500;
if (statusCode == 200) {
System.out.println("Success");
} else if (statusCode == 301 || statusCode == 302) {
System.out.println("Redirect");
} else if (statusCode == 404) {
System.out.println("Not Found");
} else if (statusCode >= 500) {
System.out.println("Server Error");
} else {
System.out.println("Unknown status: " + statusCode);
}
}
}public class NestedIf {
public static void main(String[] args) {
boolean isLoggedIn = true;
String role = "admin";
if (isLoggedIn) {
if (role.equals("admin")) {
System.out.println("Show admin dashboard");
} else {
System.out.println("Show user dashboard");
}
} else {
System.out.println("Redirect to login");
}
}
}The ternary operator is a one-line if/else. Syntax: condition ? valueIfTrue : valueIfFalse
public class Ternary {
public static void main(String[] args) {
int score = 75;
String result = score >= 60 ? "PASS" : "FAIL";
System.out.println(result); // PASS
// Same as:
// String result;
// if (score >= 60) { result = "PASS"; } else { result = "FAIL"; }
// Useful in assertions
int itemCount = 0;
String message = itemCount == 0 ? "Cart is empty" : itemCount + " items in cart";
System.out.println(message); // Cart is empty
}
}public class IfMistakes {
public static void main(String[] args) {
// MISTAKE 1: Using = instead of ==
int x = 5;
// if (x = 10) { } // Compile error — assignment, not comparison
if (x == 10) { } // Correct
// MISTAKE 2: Comparing Strings with ==
String a = new String("test");
String b = new String("test");
System.out.println(a == b); // false — different objects
System.out.println(a.equals(b)); // true — same content
// MISTAKE 3: Semicolon after if
if (x == 5);
{
System.out.println("This ALWAYS runs — semicolon ended the if");
}
}
}A semicolon after if (condition); makes the if statement do nothing. The block below it runs unconditionally. This is a silent bug — no compile error.
Q: Can you have an if without an else?
A: Yes. The else block is optional. An if without else simply does nothing when the condition is false. Use else only when you need to handle the alternative case explicitly.
Exercise 1: Write a program that takes an HTTP status code and prints the category: 1xx (Informational), 2xx (Success), 3xx (Redirect), 4xx (Client Error), 5xx (Server Error).
Exercise 2: Write a login validator: check if username is not empty, password length is >= 8, and password contains at least one digit. Print specific error messages for each failure.
Exercise 3: Using the ternary operator, write a one-liner that sets a String to "Mobile" if screen width < 768, "Tablet" if < 1024, or "Desktop" otherwise. (Hint: you can nest ternary operators.)