Java 是否支持Goto语句

Java 是否支持Goto语句

Java以其一致性和多用途而闻名。Java提供了几种控制流主要方法。Java的语法结构不支持控制流说明,如“goto”语句所示。在本部分中,我们将看看为什么Java没有goto函数,它的一些选项以及如何使用它们实现类似的目标。

语法

首先,让我们来看看Java的语法结构。goto语句允许您根据名称自由交换代码部分。在C和C++中,goto生成复杂的控制流,但代码经常难以阅读和毫无意义。

label: {
   // Code section 1
   if (condition) {
      // Code section 2
      if (anotherCondition) {
         // Code section 3
         break label;
      } else {
         // Code section 4
         if (yetAnotherCondition) {
            // Code section 5
            if (finalCondition) {
               // Code section 6
            }
         }
      }
   }
   // Code section 7
}

语法解释

Java的作者省略了goto语句,因为它会让代码变得混乱且难以理解。他们倾向于使用结构化控制流来编写更清晰、更少出错的代码。

步骤

Java中用于管理控制流的逐步算法 –

  • 入口点 - 程序将从选择的入口点开始执行,可以是main方法或其他入口点。

  • 按顺序执行 - 代码按照连续的方式逐行运行,除非遇到控制流说明,此时执行会跳转到程序中的下一个断言。

  • 创建循环的语句 – 循环语句,包括for、while和do-while语句,允许重复执行一段代码,直到满足一定条件。

方法

尽管Java没有goto,但开发人员已经找到了一些方式来实现类似的功能。

方法1:使用标签和条件语句

解释

使用标签可以标记代码段,使用条件表达式可以根据条件控制执行。而goto缺乏控制性和可读性。

示例

public class GotoExample {
   public static void main(String[] args) {
      GotoExample program = new GotoExample();
      program.execute();
   }

   private void execute() {
      label: {
         System.out.println("Executing Code Section 1");
         // Code section 1
         if (condition) {
            System.out.println("Executing Code Section 2");
            // Code section 2
            if (anotherCondition) {
               System.out.println("Executing Code Section 3");
               // Code section 3
                  break label;
            } else {
               System.out.println("Executing Code Section 4");
               // Code section 4
               if (yetAnotherCondition) {
                  System.out.println("Executing Code Section 5");
                  // Code section 5
                  if (finalCondition) {
                     System.out.println("Executing Code Section 6");
                     // Code section 6
                  }
               }
            }
         }
         System.out.println("Executing Code Section 7");
         // Code section 7
      }
   }

   private boolean condition = true;
   private boolean anotherCondition = true;
   private boolean yetAnotherCondition = true;
   private boolean finalCondition = true;
}

输出

Executing Code Section 1
Executing Code Section 2
Executing Code Section 3

解释

为了演示执行,我们在每个代码部分插入了System.out.println()语句。这样可以跟踪执行并查看根据情况运行的内容。控制中心显示代码执行消息。

方法2:封装方法

通过将代码封装在方法中来构造Java控制流。通过将软件分解为可管理的部分,方法的调用让我们可以浏览其中的内容。

示例

public class GotoExample {
   public static void main(String[] args) {
      GotoExample program = new GotoExample();
      program.execute();
   }

   private void execute() {
      section1();
      if (condition()) {
         section2();
         if (anotherCondition()) {
            section3();
            return;
         } else {
            section4();
            if (yetAnotherCondition()) {
               section5();
               if (finalCondition()) {
                  section6();
               }
            }
         }
      }
      section7();
   }

   private void section1() {
      System.out.println("Executing Code Section 1");
      // Code section 1
   }

   private void section2() {
      System.out.println("Executing Code Section 2");
      // Code section 2
   }

   private void section3() {
      System.out.println("Executing Code Section 3");
      // Code section 3
   }

   private void section4() {
      System.out.println("Executing Code Section 4");
      // Code section 4
   }

   private void section5() {
      System.out.println("Executing Code Section 5");
      // Code section 5
   }

   private void section6() {
      System.out.println("Executing Code Section 6");
      // Code section 6
   }

   private void section7() {
      System.out.println("Executing Code Section 7");
      // Code section 7
   }

   private boolean condition() {
      // Define the condition logic
      return true;
   }

   private boolean anotherCondition() {
      // Define the anotherCondition logic
      return true;
   }

   private boolean yetAnotherCondition() {
      // Define the yetAnotherCondition logic
      return true;
   }

   private boolean finalCondition() {
      // Define the finalCondition logic
      return true;
   }
}

输出

Executing Code Section 1
Executing Code Section 2
Executing Code Section 3

解释

我添加了section3()、section4()、section5()、section6()和section7()方法。Condition、anotherCondition、yetAnotherCondition和finalCondition被它们的程序替代。这些方法适用于您。

方法3:状态机

状态机管理复杂的控制流。状态机用数学方式表示转换。状态机组织控制流。

示例

enum State {
   STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, STATE_7
}

public class GotoExample {
   public static void main(String[] args) {
      GotoExample program = new GotoExample();
      program.execute();
   }

   private void execute() {
      State currentState = State.STATE_1;

      while (currentState != State.STATE_7) {
         switch (currentState) {
            case STATE_1:
               section1();
               currentState = State.STATE_2;
               break;
            case STATE_2:
               if (condition()) {
                  section2();
                  currentState = State.STATE_3;
               } else {
                  currentState = State.STATE_4;
               }
               break;
               // Define other states and transitions
         }
      }
   }

   private void section1() {
      System.out.println("Executing Code Section 1");
      // Code section 1
   }

   private void section2() {
      System.out.println("Executing Code Section 2");
      // Code section 2
   }

   // Define other section methods and states

   private boolean condition() {
      // Define the condition logic
      return true;
   }
}

输出

Executing Code Section 1
Executing Code Section 2

解释

我们将条件推理添加到condition()的技术规范中。如果可能,更改condition()的策略组。

方法4

Java中的异常处理或使用try、catch、throw、throws和finally关键字的已检查、未检查和错误异常的示例和用法。

示例

public class GotoExample {
   public static void main(String[] args) {
      try {
         section1();
         if (condition()) {
            section2();
            if (anotherCondition()) {
               section3();
               throw new GotoException();
            } else {
               section4();
               if (yetAnotherCondition()) {
                  section5();
                  if (finalCondition()) {
                     section6();
                     throw new GotoException();
                  }
               }
            }
         }
         section7();
      } catch (GotoException e) {
         // Handle the exception to continue execution
         // or perform any necessary operations
      }
   }

   private static void section1() {
      System.out.println("Executing Code Section 1");
      // Code section 1
   }

   private static void section2() {
      System.out.println("Executing Code Section 2");
      // Code section 2
   }

   private static void section3() {
      System.out.println("Executing Code Section 3");
      // Code section 3
   }

   private static void section4() {
      System.out.println("Executing Code Section 4");
      // Code section 4
   }

   private static void section5() {
      System.out.println("Executing Code Section 5");
      // Code section 5
   }

   private static void section6() {
      System.out.println("Executing Code Section 6");
      // Code section 6
   }

   private static void section7() {
      System.out.println("Executing Code Section 7");
      // Code section 7
   }

   private static boolean condition() {
      // Define the condition logic
      return true;
   }

   private static boolean anotherCondition() {
      // Define the anotherCondition logic
      return true;
   }

   private static boolean yetAnotherCondition() {
      // Define the yetAnotherCondition logic
      return true;
   }

   private static boolean finalCondition() {
      // Define the finalCondition logic
      return true;
   }
}

class GotoException extends Exception {
   // Custom exception class to simulate the "goto" behavior
}

输出

Executing Code Section 1
Executing Code Section 2
Executing Code Section 3

解释

第四种方法复制跳转声明来处理异常情况。为了”跳转”到代码,我们发出GotoException。尝试使用GotoException控制执行。

结论

即使没有goto语句,Java的有序控制流方法依然有效。标记、限定语句、状态机和有序编程范式有助于工程师编写无错误、高效的代码。要创建可信赖、易于理解的Java程序,您必须对这些可能性持开放态度并遵循最佳实践。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程