Back to Course

Data Structures and Algorithms with Java

0% Complete
0/0 Steps

The ability to create programs that can react to varying inputs is a crucial part of computer programming. Control flow statements allow us to dictate the flow of a program by controlling the order in which certain parts of code are executed. This article will explore the different types of control flow statements available in Java, how to use them, and how they work together to create powerful programs.

What are Control Flow Statements?

Control flow statements are a set of commands in a programming language that control the order in which certain parts of code are executed. These commands allow us to determine which lines of code will run and when.

In Java, there are four main types of control flow statements. These are:

  • Conditional Statements
  • Loops
  • Exception Handling
  • Switch Statements

We will explore each of these in detail below.

Conditional Statements

Conditional statements are used to control the flow of a program based on the result of a boolean expression. In Java, the main conditional statements are the “if”, “else”, and “else if” statements.

The “if” statement is used to execute a block of code if a certain condition is true. For example, the following code will print “The number is greater than 5” if the variable “num” is greater than 5:

int num = 7;

if (num > 5) {
  System.out.println("The number is greater than 5");
}

The “else” statement is used to execute a block of code if the condition in the “if” statement is false. For example, the following code will print “The number is not greater than 5” if the variable “num” is not greater than 5:

int num = 4;

if (num > 5) {
  System.out.println("The number is greater than 5");
} else {
  System.out.println("The number is not greater than 5");
}

The “else if” statement is used to check for multiple conditions. For example, the following code will print “The number is between 5 and 10” if the variable “num” is between 5 and 10:

int num = 8;

if (num > 10) {
  System.out.println("The number is greater than 10");
} else if (num > 5 && num <= 10) {
  System.out.println("The number is between 5 and 10");
} else {
  System.out.println("The number is less than 5");
}

Loops

Loops are used to execute a block of code multiple times. In Java, the main looping statements are the “while” and “for” loops.

The “while” loop is used to execute a block of code while a certain condition is true. For example, the following code will print the numbers from 1 to 10:

int num = 1;

while (num <= 10) {
  System.out.println(num);
  num++;
}

The “for” loop is used to execute a block of code a certain number of times. For example, the following code will print the numbers from 1 to 10:

for (int num = 1; num <= 10; num++) {
  System.out.println(num);
}

Exception Handling

Exception handling is used to catch and handle errors that may occur in a program. In Java, the main exception handling statements are the “try”, “catch”, and “finally” statements.

The “try” statement is used to execute a block of code that may throw an exception. For example, the following code will attempt to divide two numbers and will print the result if successful:

int num1 = 10;
int num2 = 0;

try {
  int result = num1 / num2;
  System.out.println("The result is: " + result);
} catch (Exception e) {
  System.out.println("An error occurred");
}

The “catch” statement is used to handle any exceptions that may be thrown by the “try” statement. For example, the following code will handle any exceptions that may be thrown by the previous example:

int num1 = 10;
int num2 = 0;

try {
  int result = num1 / num2;
  System.out.println("The result is: " + result);
} catch (Exception e) {
  System.out.println("An error occurred");
}

The “finally” statement is used to execute a block of code regardless of whether an exception is thrown or not. For example, the following code will execute the “cleanup()” method regardless of whether an exception is thrown or not:

int num1 = 10;
int num2 = 0;

try {
  int result = num1 / num2;
  System.out.println("The result is: " + result);
} catch (Exception e) {
  System.out.println("An error occurred");
} finally {
  cleanup();
}

Switch Statements

Switch statements are used to execute a block of code based on the value of a given expression. In Java, the main switch statement is the “switch” statement.

int num = 5;

switch (num) {
  case 1:
    System.out.println("The number is 1");
    break;
  case 5:
    System.out.println("The number is 5");
    break;
  case 10:
    System.out.println("The number is 10");
    break;
  default:
    System.out.println("The number is not 1, 5, or 10");
    break;
}

Conclusion

Control flow statements are an essential part of programming in Java. They allow us to control the flow of a program by controlling the order in which certain parts of code are executed. In this article, we explored the four main types of control flow statements available in Java: conditional statements, loops, exception handling, and switch statements.

Exercises

Write a program that will print out the numbers from 1 to 10 using a for loop.

for (int i = 1; i <= 10; i++) {
  System.out.println(i);
}

Write a program that will print out the numbers from 1 to 10 only if the number is even.

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

Write a program that will print out the numbers from 1 to 10 only if the number is divisible by 3.

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

Write a program that will print out the numbers from 1 to 10 only if the number is divisible by both 2 and 3.

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

Write a program that will print out the numbers from 1 to 10 only if the number is not divisible by 4.

for (int i = 1; i <= 10; i++) {
  if (i % 4 != 0) {
    System.out.println(i);
  }
}