Control Statements in Java

by Jasleen Chhabra | Updated on 24 August 2024

Table of Contents (Click any Topic to view first)

  1.  Decision-Making Statements
  2. Loop Statements
  3. Jump Statements

In Java programming, the compiler typically executes code from top to bottom, following the sequence in which statements are written. However, Java offers control flow statements that enable programmers to control the execution flow of their code. These statements are known as control flow statements and are fundamental to Java programming.

Decision-Making Statements:

Decision-making statements determine which statement to execute based on specific conditions. In Java, there are two main types of decision-making statements: if statements and switch statements.

  1. If Statement: The if statement evaluates a condition and executes a block of code if the condition is true.

Syntax: 

  • if (condition) {
  •     // Code block executed if condition is true
  • }

Example: 

  1. public class IfExample {
  2.     public static void main(String[] args) {
  3.         int x = 10;
  4.         int y = 12;
  5.         if (x + y > 20) {
  6.             System.out.println("x + y is greater than 20");
  7.         }
  8.     }
  9. }

 

  1. Switch Statement: The switch statement evaluates an expression and executes code blocks based on different values of the expression.

Syntax: 

  • switch (expression) {
  •     case value1:
  •         // Code block executed if expression equals value1
  •         break;
  •     // Other cases...
  •     default:
  •         // Code block executed if expression doesn't match any case
  • }

 

Example:

  1. public class SwitchExample {
  2.     public static void main(String[] args) {
  3.         int day = 2;
  4.         String dayName;
  5.         switch (day) {
  6.             case 1:
  7.                 dayName = "Monday";
  8.                 break;
  9.             // Other cases...
  10.             default:
  11.                 dayName = "Unknown";
  12.         }
  13.         System.out.println("Day is: " + dayName);
  14.     }
  15. }

Loop Statements:

Loop statements allow you to repeat a block of code multiple times. Java provides several loop statements: for, while, do-while, and for-each.

  1. For Loop: The for loop executes a block of code a specified number of times.

Syntax: 

  • for (initialization; condition; increment/decrement) {
  •     // Code block to be executed
  • }

Example: 

  1. public class ForLoopExample {
  2.     public static void main(String[] args) {
  3.         int sum = 0;
  4.         for (int j = 1; j <= 10; j++) {
  5.             sum = sum + j;
  6.         }
  7.         System.out.println("The sum of first 10 natural numbers is " + sum);
  8.     }
  9. }

 

  1. While Loop: The while loop repeats a block of code as long as a condition is true.

Syntax: 

  • while (condition) {
  •     // Code block to be executed
  • }

Example: 

  1. public class WhileLoopExample {
  2.     public static void main(String[] args) {
  3.         int i = 0;
  4.         System.out.println("Printing the list of first 10 even numbers \n");
  5.         while (i <= 10) {
  6.             System.out.println(i);
  7.             i = i + 2;
  8.         }
  9.     }
  10. }

 

  1. Do-While Loop: The do-while loop is similar to the while loop, but it guarantees at least one execution of the code block before checking the condition.

Syntax: 

  • do {
  •     // Code block to be executed
  • } while (condition);

Example: 

  1. public class DoWhileLoopExample {
  2.     public static void main(String[] args) {
  3.         int i = 0;
  4.         System.out.println("Printing the list of first 10 even numbers \n");
  5.         do {
  6.             System.out.println(i);
  7.             i = i + 2;
  8.         } while (i <= 10);
  9.     }
  10. }

 

  1. For-Each Loop: The for-each loop is used to iterate over elements in an array or collection.

Synatx: 

  • for (datatype variable : array/collection) {
  •     // Code block to be executed
  • }

Example: 

  1. public class ForEachLoopExample {
  2.     public static void main(String[] args) {
  3.         String[] names = {"Java", "C", "C++", "Python", "JavaScript"};
  4.         System.out.println("Printing the content of the array names:\n");
  5.         for (String name : names) {
  6.             System.out.println(name);
  7.         }
  8.     }
  9. }

Jump Statements:

Jump statements alter the normal flow of control in a Java program. They include the break and continue statements.

  1. Break Statement: The break statement terminates the loop or switch statement and transfers control to the statement immediately following the loop or switch.

Syntax: 

break;

Example: 

  1. public class BreakExample {
  2.     public static void main(String[] args) {
  3.         for (int i = 0; i <= 10; i++) {
  4.             System.out.println(i);
  5.             if (i == 6) {
  6.                 break;
  7.             }
  8.         }
  9.     }
  10. }

 

  1. Continue Statement: The continue statement skips the current iteration of a loop and proceeds to the next iteration.

Syntax: 

continue;

Example: 

  1. public class ContinueExample {
  2.     public static void main(String[] args) {
  3.         for (int i = 0; i <= 2; i++) {
  4.             for (int j = i; j <= 5; j++) {
  5.                 if (j == 4) {
  6.                     continue;
  7.                 }
  8.                 System.out.println(j);
  9.             }
  10.         }
  11.     }
  12. }

These control flow statements are crucial tools for Java programmers to manage the flow of execution in their programs efficiently. By understanding and using them effectively, programmers can create more flexible and dynamic Java applications.

In Java programming, decision-making statements allow you to control the flow of your program based on specific conditions. One of the fundamental decision-making statements in Java is the if-else statement. This statement evaluates a condition and executes different blocks of code depending on whether the condition is true or false. Let's explore the if-else statement in detail with examples.



FAQ

Any Questions?
Look Here.

Related Articles

Abstraction in Java

Binding in Java

Break Statement in Java

Classes & Objects in Java

Collections Framework in Java

Comments in Java

Continue Statement in Java

Data Types in Java

Do While-Loop in Java

Encapsulation in Java

Exception Handling in Java

For-Loop In Java

Hello World Program in Java

If-else Statement in Java

Inheritance in Java

Introduction to Java

Java Database Connectivity (JDBC)

Java Development Tools and Frameworks

Java GUI (Graphical User Interface) Programming

Java I/O

Java Vs. C++

Methods and Constructors in Java

Multithreading in Java

Object Oriented Programming in Java

Operators in Java

Polymorphism in Java

Scanner Class in Java

Setting Up Java Environment

Static Keyword in Java

Super Keyword in Java

Switch Statement in Java

This Keyword in Java

Variables in Java

While-Loop in Java