gpt4 book ai didi

java - 在 do while 循环中 while 之后没有分号,即使这样代码也没有抛出语法错误或编译时错误,为什么

转载 作者:行者123 更新时间:2023-11-29 08:21:21 24 4
gpt4 key购买 nike

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int i = 0;
do while ( i < 10 )
System.out.print("The value of i is " + i);
while ( i > 10 ) ;
}
}

为什么这段代码没有抛出语法错误? while 后没有分号。

最佳答案

您的代码以这种方式被感知:

    int i = 0;
do {
while (i < 10) {
System.out.print("The value of i is " + i);
}
} while (i > 10);

do 与第二个 while 耦合,而不是第一个。通常在dowhile之间有一个 block 语句({ ... }),但是Java允许任何语句那里,包括一个 while 语句。

(顺便说一句,你的内部循环是一个无限循环,所以你的代码片段不会终止。但这不是你要问的。)

关于java - 在 do while 循环中 while 之后没有分号,即使这样代码也没有抛出语法错误或编译时错误,为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com