Tutorials

The Java Tutorial: Control Flow

Now that you're familiar with primitive data types and arrays, it’s time to dive into control flow statements in Java, which allow you to direct the execution path of your programs.

Key Takeaways

  • Control flow statements in Java help manage the execution path of your code.
  • Use decision-making statements like if-then and switch for conditional logic.
  • Employ loops with for and while to repeat actions until conditions change.
  • Branching statements like break and continue modify loop execution.

What is a Control Flow Statement?

Control flow statements facilitate the decision-making processes within your applications. Here's a basic if-then example:

public class MyClass {
public static void main(String[] args) {
int i = 5;
if (i < 10) {
System.out.println("i is less than 10");
}
}
}

This if statement checks if i is less than 10 and prints a message if true. Let’s explore the types of control flow statements in Java.

Decision Making Statements

if..then

This fundamental decision-making structure executes code if a condition is true:

public class MyClass {
public static void main(String[] args) {
boolean isActive = true;
if (isActive) {
System.out.println("Your account is active");
}
}
}

Here, the code block executes because isActive is true. While braces {} are optional, they enhance readability, especially with nested logic.

if..then..else

For scenarios requiring an alternative path, use if..then..else:

public class MyClass {
public static void main(String[] args) {
boolean isActive = false;
if (isActive) {
System.out.println("Your account is active");
} else {
System.out.println("Account not found");
}
}
}

This example defaults to the else block since isActive is false.

switch

When checking a variable against multiple values, switch offers a cleaner alternative:

public class MyClass {
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1: System.out.println("The number is 1"); break;
case 2:
System.out.println("The number is 2");
System.out.println("That's an even number!");
break;
case 3: System.out.println("The number is 3"); break;
default: System.out.println("Pick a number between 1 and 3"); break;
}
}
}

The switch statement checks for equality with preset values and requires careful use of break to prevent fall-through.

Looping Statements

while

while repeats actions until a condition becomes false:

public class MyClass {
public static void main(String[] args) {
int count = 1;
while (count < 10) {
System.out.println(count);
count++;
}
}
}

This loop prints numbers 1 through 9. Forgetting count++ would create an infinite loop.

do..while

do..while guarantees the block executes at least once:

public class MyClass {
public static void main(String[] args) {
int count = 11;
do {
System.out.println(count);
count++;
} while (count < 10);
}
}

The above example executes once before terminating, despite the condition.

for

for efficiently iterates through a sequence:

public class MyClass {
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
System.out.println(i);
}
}
}

This for loop prints numbers 1 to 5, demonstrating initialization, condition, and increment.

Branching Statements

break

Terminate loops prematurely using break:

public class MyClass {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
System.out.println(i);
if (i > 2) {
break;
}
}
}
}

This prints 1 through 3, breaking before i equals 5.

continue

continue skips the current loop iteration:

public class MyClass {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
}
}

This skips printing the number 2.

FAQ

When should I use switch over if-else?

Use switch when comparing a single variable against multiple known values for readability and efficiency. Opt for if-else when evaluating ranges or more complex conditions.

Why might I choose do..while over while?

Choose do..while when you want the loop body to run at least once, regardless of the condition's initial state.

Are labeled breaks often used in practice?

Labeled breaks are less common and typically used to exit nested loops from within an inner loop, simplifying logic and enhancing readability.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews