Saturday, January 4, 2025
HomeTechTypes of Statements in Java

Types of Statements in Java

In Java, statements are instructions that tell the program what to do. They form the building blocks of a Java program and are used to perform various tasks such as controlling the flow of execution, declaring variables, or performing computations. Java statements can be categorized into several types:

1. Declaration Statements

  • Used to declare variables and specify their data types.
  • Example:
    java
    int number = 10;
    String name = "Java";

2. Expression Statements

  • Used to evaluate expressions, assign values, or call methods.
  • Examples:
    java
    x = x + 1; // Assignment
    System.out.println("Hello"); // Method call

3. Control Flow Statements

These statements control the flow of execution in a program. They are divided into:

See also  Query comparing dates in SQL

a. Decision-Making Statements

  • Used to make decisions based on conditions.
  • Examples:
    • if, else, else if:
      java
      if (x > 0) {
      System.out.println("Positive");
      } else {
      System.out.println("Negative");
      }
    • switch:
      java
      switch (day) {
      case 1: System.out.println("Monday"); break;
      case 2: System.out.println("Tuesday"); break;
      default: System.out.println("Invalid day");
      }

b. Looping Statements

  • Used for repetitive execution of code.
  • Examples:
    • for:
      java
      for (int i = 0; i < 5; i++) {
      System.out.println(i);
      }
    • while:
      java
      while (x < 10) {
      x++;
      }
    • do-while:
      java
      do {
      x++;
      } while (x < 10);

c. Jump Statements

  • Used to break or continue the flow of execution.
  • Examples:
    • break:
      java
      for (int i = 0; i < 10; i++) {
      if (i == 5) break;
      System.out.println(i);
      }
    • continue:
      java
      for (int i = 0; i < 10; i++) {
      if (i == 5) continue;
      System.out.println(i);
      }
    • return:
      Used to exit from a method and optionally return a value:

      java
      return x + y;

4. Block Statements

  • Enclose multiple statements within {} to group them as a block.
  • Example:
    java
    {
    int x = 5;
    int y = 10;
    System.out.println(x + y);
    }

5. Try-Catch-Finally Statements

  • Used for exception handling in Java.
  • Example:
    java
    try {
    int result = 10 / 0;
    } catch (ArithmeticException e) {
    System.out.println("Error: Division by zero");
    } finally {
    System.out.println("Execution completed");
    }

6. Synchronized Statements

  • Used in multi-threading to control access to critical sections of code.
  • Example:
    java
    synchronized (this) {
    System.out.println("Thread-safe block");
    }

Conclusion

Java provides various types of statements to control program flow, perform operations, and handle exceptions. Understanding these statements is essential for writing efficient and structured Java programs.

RELATED ARTICLES

Leave a Reply

- Advertisment -

Most Popular

Who is PVD Biggie? 

Celebrities Born in 1967

Who is Kaylee Hottle? 

Who is Jackie Christie?

Recent Comments