Java 支持 Goto 吗?

javaobject oriented programmingprogramming更新于 2024/11/3 13:00:17

Java 以其一致性和多功能性而闻名。Java 提供了几种控制流主要方法。Java 的词源结构缺乏控制流解释,如"goto"语句所示。在本部分中,我们将研究 Java 没有 goto 函数的原因、它的一些选项以及如何使用它们来实现类似的目标。

语法

首先,让我们检查一下 Java 的语言结构。goto 解释使您能够根据名称自由交换代码部分。Goto 在 C 和 C++ 中生成复杂的控制流,但代码通常不可读且毫无价值。

label: {
   // 代码部分 1
   if (condition) {
      // 代码部分 2
      if (anotherCondition) {
         // 代码部分 3
         break label;
      } else {
         // 代码部分 4
         if (yetAnotherCondition) {
            // 代码部分 5
            if (finalCondition) {
               // 代码部分 6
            }
         }
      }
   }
   // 代码部分 7
}

语法解释

Java 的作者省略了 goto 表达,因为它会使代码混乱并难以理解。他们倾向于结构化的控制流,以便使代码更清晰,错误更少。

算法

Java 中用于管理控制流的分步算法 -

  • 入口点- 程序的执行将从所选的入口点开始,该入口点可能是主方法或另一个入口点。

  • 按顺序执行 - 代码以连续的方式逐行运行,除非遇到控制流解释,在这种情况下执行会跳转到程序中的以下断言。

  • 创建循环的语句循环语句(包括 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");
         // 代码部分 1
         if (condition) {
            System.out.println("Executing Code Section 2");
            // 代码部分 2
            if (anotherCondition) {
               System.out.println("Executing Code Section 3");
               // 代码部分 3
                  break label;
            } else {
               System.out.println("Executing Code Section 4");
               // 代码部分 4
               if (yetAnotherCondition) {
                  System.out.println("Executing Code Section 5");
                  // 代码部分 5
                  if (finalCondition) {
                     System.out.println("Executing Code Section 6");
                     // 代码部分 6
                  }
               }
            }
         }
         System.out.println("Executing Code Section 7");
         // 代码部分 7
      }
   }

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

输出

执行代码部分 1
执行代码部分 2
执行代码部分 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("执行代码部分 1");
    // 代码部分 1
    }
    
    private void section2() {
    System.out.println("执行代码部分 2");
    // 代码部分 2
    }
    
    private void section3() {
    System.out.println("执行代码部分 3");
    // 代码部分 3
    }
    
    private void section4() {
    System.out.println("执行代码部分 4");
    // 代码部分 4
    }
    
    private void section5() {
    System.out.println("执行代码部分 5");
    // 代码部分 5
    }
    
    private void section6() {
    System.out.println("执行代码部分 6");
    // 代码部分 6
    }
    
    private void section7() {
    System.out.println("执行代码部分 7");
    // 代码部分 7
    }
    
    private boolean condition() {
    // 定义条件逻辑
    return true;
    }
    
    private boolean anotherCondition() {
    // 定义 anotherCondition 逻辑
    return true;
    }
    
    private boolean yetAnotherCondition() {
    // 定义 yetAnotherCondition 逻辑
    return true;
    }
    
    private boolean finalCondition() {
    // 定义 finalCondition 逻辑
    return true;
    }
}

输出

执行代码部分 1
执行代码部分 2
执行代码部分 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("执行代码部分 1");
        // 代码部分 1
    }
    
    private void section2() {
        System.out.println("执行代码部分 2");
        // 代码部分 2
    }
    
    // 定义其他部分方法和状态
    
    private boolean condition() {
        // 定义条件逻辑
        return true;
    }
}

输出

执行代码部分 1
执行代码部分 2

解释

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

方法4异常处理

Java中的异常处理或带有checked,unchecked和错误的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("执行代码部分 1");
    // 代码部分 1
    }
    
    private static void section2() {
    System.out.println("执行代码部分 2");
    // 代码部分 2
    }
    
    private static void section3() {
    System.out.println("执行代码部分 3");
    // 代码部分 3
    }
    
    private static void section4() {
    System.out.println("执行代码部分 4");
    // 代码部分 4
    }
    
    private static void section5() {
    System.out.println("执行代码部分 5");
    // 代码部分 5
    }
    
    private static void section6() {
    System.out.println("执行代码部分 6");
    // 代码部分 6
    }
    
    private static void section7() {
    System.out.println("执行代码部分 7");
    // 代码部分 7
    }
    
    private static boolean condition() {
    // 定义条件逻辑
    return true;
    }
    
    private static boolean anotherCondition() {
    // 定义 anotherCondition 逻辑
    return true;
    }
    
    private static boolean yetAnotherCondition() {
    // 定义 yetAnotherCondition 逻辑
    return true;
    }
    
    private static boolean finalCondition() {
    // 定义 finalCondition 逻辑
    return true;
    }
}

class GotoException extends Exception {
   // 自定义异常类来模拟"goto"行为
}

输出

执行代码部分 1
执行代码部分 2
执行代码部分 3

解释

方法 4 复制 goto 语句来处理异常情况。要"跳"到代码,我们发出 GotoException。尝试使用 GotoException 控制执行。

结论

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


相关文章


有用资源