Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /home/bibiizjb/ptutorials.com/en/account/functions/check_point_2.php on line 25
Java Control Flow Tutorial

Java Control Flow Tutorial

Control flow statements in Java allow you to control the execution of your program based on certain conditions or loops. This tutorial covers the three main types of control flow statements: conditional statements (if, else, switch), loops (for, while, do-while), and branching statements (break, continue, return).

By the end of this tutorial, you'll understand how to use control flow statements to make decisions, repeat tasks, and manage the flow of your Java programs.

Conditional Statements

Conditional statements allow you to execute different blocks of code based on specific conditions.

  • if Statement:
    • Executes a block of code if a condition is true.
    • Syntax:
      if (condition) {
          // Code to execute if condition is true
      }
    • Example:
      int x = 10;
      if (x > 5) {
          System.out.println("x is greater than 5");
      }
  • if-else Statement:
    • Executes one block of code if the condition is true and another block if it is false.
    • Syntax:
      if (condition) {
          // Code to execute if condition is true
      } else {
          // Code to execute if condition is false
      }
    • Example:
      int x = 3;
      if (x > 5) {
          System.out.println("x is greater than 5");
      } else {
          System.out.println("x is less than or equal to 5");
      }
  • if-else-if Ladder:
    • Used to test multiple conditions.
    • Syntax:
      if (condition1) {
          // Code to execute if condition1 is true
      } else if (condition2) {
          // Code to execute if condition2 is true
      } else {
          // Code to execute if all conditions are false
      }
    • Example:
      int x = 7;
      if (x > 10) {
          System.out.println("x is greater than 10");
      } else if (x > 5) {
          System.out.println("x is greater than 5 but less than or equal to 10");
      } else {
          System.out.println("x is less than or equal to 5");
      }
  • switch Statement:
    • Used to select one of many code blocks to execute based on the value of a variable.
    • Syntax:
      switch (variable) {
          case value1:
              // Code to execute if variable == value1
              break;
          case value2:
              // Code to execute if variable == value2
              break;
          default:
              // Code to execute if variable doesn't match any case
      }
    • Example:
      int day = 3;
      switch (day) {
          case 1:
              System.out.println("Monday");
              break;
          case 2:
              System.out.println("Tuesday");
              break;
          case 3:
              System.out.println("Wednesday");
              break;
          default:
              System.out.println("Invalid day");
      }

Loops

Loops allow you to repeat a block of code multiple times.

  • for Loop:
    • Used when the number of iterations is known.
    • Syntax:
      for (initialization; condition; update) {
          // Code to execute
      }
    • Example:
      for (int i = 0; i < 5; i++) {
          System.out.println("i = " + i);
      }
  • while Loop:
    • Used when the number of iterations is not known and depends on a condition.
    • Syntax:
      while (condition) {
          // Code to execute
      }
    • Example:
      int i = 0;
      while (i < 5) {
          System.out.println("i = " + i);
          i++;
      }
  • do-while Loop:
    • Similar to the while loop, but the code block is executed at least once.
    • Syntax:
      do {
          // Code to execute
      } while (condition);
    • Example:
      int i = 0;
      do {
          System.out.println("i = " + i);
          i++;
      } while (i < 5);

Branching Statements

Branching statements allow you to control the flow of loops and switch statements.

  • break:
    • Used to exit a loop or switch statement.
    • Example:
      for (int i = 0; i < 10; i++) {
          if (i == 5) {
              break; // Exit the loop when i == 5
          }
          System.out.println("i = " + i);
      }
  • continue:
    • Used to skip the current iteration of a loop and proceed to the next iteration.
    • Example:
      for (int i = 0; i < 5; i++) {
          if (i == 2) {
              continue; // Skip iteration when i == 2
          }
          System.out.println("i = " + i);
      }
  • return:
    • Used to exit a method and optionally return a value.
    • Example:
      public int add(int a, int b) {
          return a + b; // Return the sum of a and b
      }

This tutorial covered the essential control flow statements in Java. Practice using these statements to build dynamic and efficient programs.

0 Interaction 0 Views 0 likes
Heart Button
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials, but please remember that tutorials may not be 100% accurate, as occasional mistakes can happen. Once you've mastered the language, we highly recommend consulting the official documentation to stay updated with the latest changes. If you spot any errors, please feel free to report them to help us improve.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home