草庐IT

Java do while, while

coder 2024-03-11 原文

当我运行这段代码时,我可以期待什么行为:

do while(testA) {

    // do stuff

} while(testB);

它会像这样吗:

do {
    while(testA) {
        // do stuff
    }    
} while(testB);

或者:

if(testA) {
    do {
        // do stuff
    } while(testA && testB);
}

还是完全出乎意料的事情?

我问这个问题是因为我觉得这很模棱两可,对于搜索这个主题的其他人来说,不是因为我懒得测试它。

最佳答案

它相当于你的第一个 block :

do {
    while(testA) {
        // do stuff
    }    
} while(testB);

Java grammar的相关部分解析时是:

DoStatement:
    do Statement while ( Expression ) ;

Statement:
    WhileStatement

WhileStatement:
    while ( Expression ) Statement

Statement:
    Block

Block:
    { BlockStatements_opt }

您可以看到 Java 编译器会将其解析为 do <WhileStatement> while ( Expression ) ; .这是解析您编写的代码的唯一有效方法。

请记住,它没有任何特殊规则来解析此构造。由于编写 do-while 循环的不寻常方式,它最终会让人难以阅读。在正常使用中,do-while 始终写为 do { ... } while带有显式花括号。

关于Java do while, while,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2434541/

有关Java do while, while的更多相关文章

  1. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  2. ruby - 从 Ruby : capturing the output while displaying the output? 运行 shell 命令 - 2

    我有一个问题。我想从另一个ruby​​脚本运行一个ruby​​脚本并捕获它的输出信息,同时让它也输出到屏幕。亚军#!/usr/bin/envrubyprint"Enteryourpassword:"password=gets.chompputs"Hereisyourpassword:#{password}"我运行的脚本文件:开始.rboutput=`runner`putsoutput.match(/Hereisyour(password:.*)/).captures[0].to_s正如您在此处看到的那样,存在问题。在start.rb的第一行,屏幕是空的。我在运行程序中看不到“输入您的密

  3. 一行中的 while block 的 Ruby 语法 - 2

    请问,一行whileblock的Ruby语法是什么? 最佳答案 例如putsa[i+=1]whilei 关于一行中的whileblock的Ruby语法,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/21712232/

  4. ruby - Rubocop 规则 : Never use 'do' with multi-line 'while - 2

    我有以下代码#coloursarandomcellwithacorrectcolourdefcolour_random!whiletruedocol,row=rand(columns),rand(rows)cell=self[row,col]ifcell.empty?thencell.should_be_filled??cell.colour!(1):cell.colour!(0)breakendendend做什么并不重要,尽管它应该很明显。关键是Rubocop给了我一个警告Neveruse'do'withmulti-line'while为什么我不应该那样做?那我该怎么办呢?

  5. ruby - 无法加载此类文件——脚本/rails : Getting this error while remote debugging through RubyMine - 2

    我在通过RubyMineIDE进行远程调试时遇到以下错误。$bundleexecrdebug-ide--port1234--script/railsserverFastDebugger(ruby-debug-ide0.4.9)listenson:1234/home/amit/.rvm/gems/ruby-1.9.3-p125/gems/ruby-debug-ide19-0.4.12/lib/ruby-debug-ide.rb:123:in`debug_load'/home/amit/.rvm/gems/ruby-1.9.3-p125/gems/ruby-debug-ide19-0.4.

  6. Ruby - 为什么使用 "until"而 "while"可以做同样的事情 - 2

    很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭9年前。这是使用while的代码:i=0num=5whilei这是使用until的代码:i=0num=5untili>numdoputs"Insidetheloop!i=#{i}"i+=1end有人可以举例说明什么时候应该首选一个而不是另一个吗?对我来说,如果他们做同样的事情,就没有理由使用until和while。在我看来,这就是为什么其他编程语言没有两者的原因。

  7. ruby - 为什么尾递归 gcd 比 rubinius 的 while 循环更快 - 2

    我有这两个gcd函数的实现:defgcd1(a,b)ifa==baelsifa>bif(a%b)==0belsegcd1(a%b,b)endelseif(b%a)==0aelsegcd1(a,b%a)endendenddefgcd2(a,b)if(a==b)returnaelsifb>amin,max=a,belsemin,max=b,aendwhile(max%min)!=0min,max=max%min,minendminend函数gcd1是尾递归的,而gcd2使用while循环。我已经验证rubinius通过对阶乘函数进行基准测试来执行TCO,只有阶乘函数基准测试显示递归版本和迭

  8. ruby-on-rails - 目录 : template missing error while rendering JSON - 2

    我的Controller中有以下代码:deftljson(result=[])@stat_id=params[:stat_id]@rpm=FedoraRpm.find_by_name(@stat_id)@rpm.ruby_gem.historical_gems.each{|h|resulth.version,:start=>h.build_date}}@rpm.bugs.each{|b|resultb.name+"ViewonBugZilla",:start=>b.bz_id}}@res=result.to_jsonrespond_todo|format|format.json{ren

  9. ruby - 错误 : inreplace failed while installling with homebrew? - 2

    我想安装treetagger在OSX中。为了使它更容易,我尝试搜索Homebrew是否可行。所以我在网上找到了这个formula来自pepijnkokke用户。接下来,我尝试按如下方式安装treetagger:user@MacBook-Pro-User-2:~$brewinstall/Users/user/Downloads/tree-tagger.rb但是,我得到了以下错误:==>Installingdependenciesfortree-tagger:openssl,wget==>Installingtree-taggerdependency:openssl==>Downloadi

  10. ruby-on-rails - ruby rails : Removing a dependency while installing a gem? - 2

    我正在尝试在我的Windows机器上安装twitter-bootstrap-rails。这个gem依赖于therubyracer,它还不兼容Windows。我发现了这个:therubyracergemonwindows这告诉我,我应该只能依靠JScript。虽然我不知道如何告诉gem安装忽略依赖项。我试图强制安装:geminstall-ftwitter-bootstrap-rails--platformruby没有用。有没有人我怎么能说“geminstalltwitter-bootstrap-rails[但忽略对therubyracer的依赖]”? 最佳答案

随机推荐