Java中无法到达的语句

An unreachable statement in Java is a compile-time error. This error occurs when there is a statement in your code that will not be executed even once. This error points out a logical flaw in the flow of your code.

Java中无法访问的语句是编译时错误。 当您的代码中有一条语句甚至不会执行一次时,就会发生此错误。 该错误指出了代码流中的逻辑缺陷。

Such an error can arise from an infinite loop or placing code after a return or a break statement among several other causes.

这种错误可能是由于无限循环或在返回或break语句之后放置代码而引起的,还有其他几种原因。

Let’s look at a few examples of unreachable statements.

让我们看一些无法到达的语句的例子。

1.无法到达的While循环 (1. Unreachable While loop )

If the condition of a while loop is such that it is never true, then the code inside the loop will never run. This makes the code inside the while loop un-reachable.

如果while循环的条件永不为真,则循环内的代码将永远不会运行。 这使得while循环中的代码无法访问。


package com.journaldev.java;
public class Main {

    public static void main(String[] args) {
        
        while(3>5){
            System.out.println("Hello");
        }


    }

}
Unreachable While

The IDE points out the error while writing the code. That’s quite ‘intelliJ’ent.

IDE会在编写代码时指出错误。 那是相当“智能”的。

Upon running we get :

运行后,我们得到:

Error

2.无限循环后的代码 (2. Code after an infinite loop )

A piece of code just after an infinite loop will never be executed. In fact, the entire code after the infinite loop will never be executed. This should motivate you to pay attention while writing the terminating condition of a loop.

无限循环之后的一段代码将永远不会执行。 实际上,无限循环之后的整个代码将永远不会执行。 这应该激发您在编写循环的终止条件时要注意。


package com.journaldev.java;
public class Main {
    public static void main(String[] args) {
        while(true){
            System.out.println("Hello");
        }
        int a=1;
    }
}
Infinite Loop

3.中断或继续语句后的代码 (3. Code after a break or continue statement )

Break statement lets us break out of a loop. Continue statement lets us skip the current iteration and move to the next iteration Placing code after either of the two makes the statement unreachable.

Break语句使我们摆脱了循环。 Continue语句使我们可以跳过当前迭代,并移至下一个迭代。在两者之一使该语句无法访问之后,放置代码。


package com.journaldev.java;
public class Main {

    public static void main(String[] args) {

        for(int i=0;i<5;i++){
            if(i==2){
                break;
                System.out.println("Hello");
            }
        }
    }
}
After Break Statement
statement after break
休息后声明
After Continue
statement after continue
继续后的声明

如何解决Unreachable Statement错误? (How to fix an Unreachable Statement error?)

There is no particular way to fix such an error. It all depends on how good you are as a programmer. The problem is in the flow of your code.

没有解决这种错误的特定方法。 这完全取决于您作为程序员的水平。 问题出在代码流中。

Flowcharts are essential for understanding the flow of any code. You can try drawing the flowchart for the problem you are trying to address. Then you can match your code with the flowchart or write the code from scratch from this new understanding of the problem.

流程图对于理解任何代码流都是必不可少的。 您可以尝试绘制要解决的问题的流程图。 然后,您可以将代码与流程图进行匹配,或者通过对问题的新理解从头开始编写代码。

Another question this type of error could pose is whether you even need the statements that are unreachable? Maybe you don’t really need the statements, in that case, you can just go ahead and delete them.

这种错误可能引起的另一个问题是,您是否还需要无法到达的语句? 也许您实际上并不需要这些语句,在这种情况下,您可以继续删除它们。

If you are still not able to figure it out, you can always comment on this post and we’ll help you figure it out. That’s what we are here for 🙂

如果您仍然无法解决问题,则可以随时对此信息发表评论,我们将帮助您解决问题。 那就是我们在这里的🙂

翻译自: https://www.journaldev.com/42299/unreachable-statement-in-java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值