CHF -> Control Flow Statements in Java

Control flow statements – some may say it’s just another brick in the wall, but this one’s a big one. Though it may seem basic, when you delve into all the niche cases and antipatterns, it shows just how crucial it is. Not to mention, it’s used literally everywhere – in streams, method chaining, and all the code the cool kids are writing these days. So don’t underestimate the power of a humble “for loop” and really dig into today’s topic. You can thank me later.


Conditional Statements

1. if-else statement: Tests a condition and executes a block of code if the condition is true. If the condition is false, the code in the else block is executed (if present).

if (condition) {
    // code to execute if the condition is true
} else {
    // code to execute if the condition is false
}

2. switch statement: Compares a given value against multiple cases and executes the code block that corresponds to the matching case. It can also have a default case, which executes if none of the cases match.

switch (value) {
    case value1:
        // code to execute if value == value1
        break;
    case value2:
        // code to execute if value == value2
        break;
    // ...
    default:
        // code to execute if none of the cases match
}
NOTE: There are some situations where a switch statement may not be the best choice. For example, if you have a large number of cases, it can become unwieldy and difficult to maintain. Additionally, if the logic for each case is complex, it may be better to use a series of if statements instead of a switch statement.

Looping Statements

1. for loop: Repeats a block of code a specified number of times, based on a given condition and a loop counter.

for (initialisation; condition; increment) {
    // code to execute while the condition is true
}

2. while loop: Repeats a block of code as long as the specified condition is true.

while (condition) {
    // code to execute while the condition is true
}

3. do-while loop: Similar to the while loop, but the code block is executed at least once before the condition is checked.

do {
    // code to execute
} while (condition);

4. For Each loop: The “for each” loop is a simplified version of the “for” loop that iterates over arrays or other collections of elements. The “for each” loop assigns each element in the array to the variable specified, and then executes the code block for each element. This loop is commonly used when you need to iterate over a collection of elements and perform the same operation on each element.

for (type variable : array) {
    // code to execute for each element in the array
}
NOTE: For-Each Loop is considered the cool sister of the loop statements.It is more concise and readable compared to a traditional for loop. Understanding how to use the "for each" loop is crucial for working with collections in Java.

Breaking and Continuing Statements

1. break keyword: Terminates the innermost loop or switch statement and transfers control to the statement immediately after the loop or switch.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}

2. continue keyword: Skips the remaining code in the current iteration of the loop and starts the next iteration.

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    System.out.println(i);
}

Using Labeled Statements – RARE

Labels can be applied to loops or blocks, allowing you to break or continue a specific outer loop.

outerLoop:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (j == 3) {
            continue outerLoop;
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}
NOTE: It's worth mentioning that Labeled Statements are not popular among Java developers and some even treat them as an antipattern because they break the natural flow of the application.

That’s it for now, friends! I hope you discovered something valuable today. As we conclude our exploration of Java operators and expressions, remember to integrate these fundamental concepts into your coding routine.

Take it easy, and don’t worry if you can’t memorise everything at once. Focus on learning just a little bit every day, and never give up! It might feel overwhelming at times, but trust in the process, and keep pushing forward. Eventually, all the pieces will click into place, and you’ll find yourself confidently navigating the world of Java with ease. Keep at it, and happy coding!


/*
Until next time,
eMs
*/

Leave a comment