做while循环检查最后一次迭代 [英] Do while loop check for last iteration

查看:68
本文介绍了做while循环检查最后一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来像是一个基本问题,但我想检查一下我的思维过程是否正确.

This may sound like an elementary question, but I want to check whether my thought process is correct.

我有一个do while循环,我想检查循环何时进入上一次迭代,以便设置一个值或不设置一个值.我已经提供了这种解决方案,但没有感觉,我想知道是否还有另一种方法.

I have a do while loop and I want to check when the loop has entered the last iteration so as to either set a value or not. I have come with this sort solution but doesn't feel and I was wondering whether there is a another way.

 do 
  { 
      int iterationCheck ++;

      if( iterationCheck! = counter)
       // Do something 
       counter++;
   } while (true)

这是正确的还是有更好的方法?

Is this correct or is there a better way?

推荐答案

确定您可以执行此操作以计算迭代次数但是,请将"interationCheck = 0"变量声明放在do/while范围之前.

Sure you could do this to count the iteration But put the 'int iterationCheck=0' variable declaration before the do/while scope.

您也可以进行"for"循环(那会更好)

You could go for a 'for' loop too ( which would be better )

但是,这一切都取决于循环的条件.因为我们无法推断出循环的退出"状态,所以我们无法提供准确的答案.

However it all depends on the condition of your loop. Because we can't infer the 'out' condition of your loop so we can't provide a accurate answer.

这篇关于做while循环检查最后一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆