A for loop repeats a block of code a specific number of times. In automation, loops iterate through test data, retry failed actions, and process lists of elements.
public class ForBasics {
public static void main(String[] args) {
// Print numbers 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println("Test case " + i);
}
}
}Test case 1
Test case 2
Test case 3
Test case 4
Test case 5The for loop has three parts: for (init; condition; update). Init runs once before the loop starts. Condition is checked before each iteration — if false, the loop stops. Update runs after each iteration.
public class ForVariations {
public static void main(String[] args) {
// Count down
for (int i = 5; i >= 1; i--) {
System.out.println("Countdown: " + i);
}
// Step by 2
for (int i = 0; i <= 10; i += 2) {
System.out.print(i + " "); // 0 2 4 6 8 10
}
System.out.println();
// Loop through a String
String password = "Test@123";
int digitCount = 0;
for (int i = 0; i < password.length(); i++) {
if (Character.isDigit(password.charAt(i))) {
digitCount++;
}
}
System.out.println("Digits in password: " + digitCount); // 3
}
}The for-each loop (enhanced for) iterates through arrays and collections without managing an index. Use it when you need every element and do not need the index.
public class ForEach {
public static void main(String[] args) {
String[] browsers = {"Chrome", "Firefox", "Safari", "Edge"};
// for-each — cleaner when you don't need the index
for (String browser : browsers) {
System.out.println("Testing on: " + browser);
}
// When you need the index, use regular for
for (int i = 0; i < browsers.length; i++) {
System.out.println((i + 1) + ". " + browsers[i]);
}
}
}public class BreakContinue {
public static void main(String[] args) {
// break — exit the loop immediately
System.out.println("Finding first failing test:");
String[] results = {"PASS", "PASS", "FAIL", "PASS", "FAIL"};
for (int i = 0; i < results.length; i++) {
if (results[i].equals("FAIL")) {
System.out.println("First failure at index " + i);
break; // stop searching
}
}
// continue — skip current iteration, go to next
System.out.println("\nPassed tests only:");
for (int i = 0; i < results.length; i++) {
if (results[i].equals("FAIL")) {
continue; // skip failures
}
System.out.println("Test " + (i + 1) + ": " + results[i]);
}
}
}public class NestedLoops {
public static void main(String[] args) {
// Cross-browser, cross-environment testing matrix
String[] browsers = {"Chrome", "Firefox"};
String[] envs = {"dev", "staging", "prod"};
for (String browser : browsers) {
for (String env : envs) {
System.out.println("Test: " + browser + " on " + env);
}
}
}
}Test: Chrome on dev
Test: Chrome on staging
Test: Chrome on prod
Test: Firefox on dev
Test: Firefox on staging
Test: Firefox on prodQ: What is the difference between break and continue?
A: break exits the entire loop immediately — no more iterations run. continue skips the rest of the current iteration and jumps to the next iteration. break is used when you found what you need (search). continue is used to skip items that do not meet a condition (filter).
Exercise 1: Write a loop that prints the multiplication table for 7 (7 x 1 = 7, 7 x 2 = 14, ... 7 x 10 = 70).
Exercise 2: Given a String, count the number of vowels (a, e, i, o, u — both cases). Print the count.
Exercise 3: Write a program that prints all numbers from 1 to 50 that are divisible by both 3 and 5.