简体   繁体   English

break 语句的控制流如何在嵌套循环中工作? (爪哇)

[英]How does the control flow for break statements work in nested loops? (Java)

I have been programming in Java for a while, however I ran into something today that I thought was odd.我已经在 Java 编程了一段时间,但是我今天遇到了一些我认为很奇怪的事情。 I recall that when a break statement is used in a nested loop the control flow returns to the header of the outer loop.我记得当在嵌套循环中使用 break 语句时,控制流返回到外循环的 header。 As demonstrated by this graphic.如图所示。

Control Flow Diagram (I know you guys like it inline, but per reputation requirements I am not allowed)控制流程图(我知道你们喜欢内联,但根据声誉要求,我是不允许的)

Perhaps, this graphic and my recollection is incorrect.也许,这个图形和我的记忆是不正确的。 I say this because when I run the following code, the control flow adds every token to the LinkedList listOfAllPalindromes.我这样说是因为当我运行以下代码时,控制流会将每个标记添加到 LinkedList listOfAllPalindromes。 I have verified that the if-block that contains the break statement is being accessed when it is supposed to be.我已经验证了包含 break 语句的 if 块在它应该被访问的时候被访问。 So in short, is my memory wrong, is the control flow when it hits the break statement supposed to jump to listOfAllPalindromes.add(token) like it appears to be doing?简而言之,我的 memory 是不是错了,当它遇到 break 语句时的控制流是否应该像看起来那样跳转到 listOfAllPalindromes.add(token) ?

 public static void main(String[] args) throws FileNotFoundException {

    File dictionary = new File("english3.txt");
    Scanner s = new Scanner(dictionary);
    String token;
    LinkedList<String> listOfPalindromes = new LinkedList<>();

    while (s.hasNext())
    {
        token = s.next().toLowerCase();

        for (int i = 0; i <= (token.length()-1)/2; i++)
        {
            if(token.charAt(i) != token.charAt(token.length()-(i+1)))
            {
                break;
            }
        }
        
        listOfPalindromes.add(token);
    }

    for(String word : listOfPalindromes)
    {
        System.out.println(word);
    }
}

You should use continue with a label instead of break like this.您应该continue使用 label 而不是像这样break

Scanner s = new Scanner("lever level canoe kayak carrace racecar mademoiselle madam");
String token;
LinkedList<String> listOfPalindromes = new LinkedList<>();

L: while (s.hasNext()) {
    token = s.next().toLowerCase();

    for (int i = 0; i <= (token.length() - 1) / 2; i++) {
        if (token.charAt(i) != token.charAt(token.length() - (i + 1))) {
            continue L;
        }
    }

    listOfPalindromes.add(token);
}

for (String word : listOfPalindromes) {
    System.out.println(word);
}

output output

level
kayak
racecar
madam

use labels to loops for better understanding on control flow of loops.对循环使用标签以更好地理解循环的控制流。

It should jump come out of for loop as it hits break statement它应该在遇到 break 语句时跳出 for 循环

From jls-14.15 :jls-14.15

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement;没有 label 的 break 语句尝试将控制权转移到最里面的封闭开关、while、do 或 for 语句; this enclosing statement, which is called the break target, then immediately completes normally.这个封闭的语句,称为中断目标,然后立即正常完成。

So, answering your question the break statement will break your for loop, and the listOfPalindromes.add(token) line will be executed.因此,回答您的问题, break语句将中断您的 for 循环,并且listOfPalindromes.add(token)行将被执行。

You can resolve your problem using other method to check is palindrome, and then from your first looop fire this method.您可以使用其他方法解决您的问题以检查是否是回文,然后从您的第一个循环中触发此方法。 Example:例子:

public static void main(String[] args) throws FileNotFoundException {

    File dictionary = new File("english3.txt");
    Scanner s = new Scanner(dictionary);
    String token;
    LinkedList<String> listOfPalindromes = new LinkedList<>();

    while (s.hasNext())
    {
        token = s.next().toLowerCase();

        if(isPalindrome(token)) {
            listOfPalindromes.add(token);
        }
    }

    for(String word : listOfPalindromes)
    {
        System.out.println(word);
    }
}

private static boolean isPalindrome(String token) {
    for (int i = 0; i <= (token.length()-1)/2; i++)
    {
        if(token.charAt(i) != token.charAt(token.length()-(i+1)))
        {
            return false;
        }
    }
    return true;
}

I didn't check is code correctly, only resolve problem with break我没有检查代码是否正确,只解决了break问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM