0

0

Java支持Goto吗?

王林

王林

发布时间:2023-09-05 23:33:07

|

1537人浏览过

|

来源于tutorialspoint

转载

java支持goto吗?

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

语法

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

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 中用于管理控制流的分步算法 -

立即学习Java免费学习笔记(深入)”;

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

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

  • 创建循环的语句循环语句(包括 for、while 和 do-while 语句)允许重复代码块,直到满足特定条件。

方法

即使 Java 没有 goto,开发人员也找到了构建类似功能的方法。

方法 1:标签和条件语句

说明

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

同徽B2C电子商务软件系统
同徽B2C电子商务软件系统

开发语言:java,支持数据库:Mysql 5,系统架构:J2EE,操作系统:linux/Windows1. 引言 32. 系统的结构 32.1 系统概述 33. 功能模块设计说明 43.1 商品管理 43.1.1 添加商品功能模块 53.1.2 商品列表功能模块 83.1.3 商品关联功能模块 93.

下载

示例

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() 的技术规范中。如果可能,更改条件()的策略组。

方法4异常处理

Java 中的异常处理或带有已检查、未检查和错误的 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

说明

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

结论

即使没有 goto 表达,Java 的有序控制流方法也能工作。标记、有限的关节、状态机和有序编程范例可帮助工程师编写无错误、高效的代码。要创建值得信赖、易于理解的 Java 程序,您必须接受这些可能性并遵循最佳实践。

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

804

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

723

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

727

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

395

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

445

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

428

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16861

2023.08.03

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.1万人学习

C# 教程
C# 教程

共94课时 | 5.7万人学习

Java 教程
Java 教程

共578课时 | 39.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号