如何替换“while”循环“for”环? [英] How to replace "while" loop by "for" loop?

查看:128
本文介绍了如何替换“while”循环“for”环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#中的for循环替换while循环,怎么做?谢谢。

例如:

I want to replace "while" loop by "for" loop in C#, how to do this? Thanks.
E.g:

do
{
     // Do here
} while (condition);





将其转换为for循环????



convert it to "for" loop????

推荐答案

只有一种约定,for循环应该迭代固定次数,并且它可以被视为一种非常通用的通用循环语句。在大多数情况下,最好坚持使用惯例,否则代码会变得难以理解,或者换句话说容易误解。



无论如何这里有两个显示for和do while等效的示例,两者都没有典型的i ++ interation语句。



当随机数生成器给出8时,此示例终止。

It is only a convention that a for loop should iterate a fixed number of times and it can be regarded as a very versatile general purpose looping statement. It's probably best to stick with the convention in most cases otherwise code can become difficult to understand, or to put it another way, easy to misunderstand.

Anyway here are two examples showing for and do while equivalents, neither of which have the typical i++ interation statements.

This example terminates when the random number generator gives us an 8.
private static void DoWhileSimulation() {
  // Use a known seed for Random so that the sequence can be reproduced
  const Int32 Seed = 0;

  Console.WriteLine("For");
  Random rnd = new Random(Seed);
  for (Int32 i = 0; i != 8; i = rnd.Next(0, 13)) {
    Console.WriteLine(i);
  }
  Console.WriteLine();

  Console.WriteLine("Do While");
  // Set Random to generate the same sequence
  rnd = new Random(Seed);
  Int32 j = 0;
  do {
    Console.WriteLine(j);
    j = rnd.Next(0, 13);
  } while (j != 8);
  Console.WriteLine();
}





在这一个我们循环固定的持续时间



In this one we loop for a fixed duration

private static void TimeLoop() {
  // Loop for 5 seconds
  Console.WriteLine("For Time");
  for (DateTime nowf = DateTime.Now, endTimef = nowf.AddSeconds(5); nowf < endTimef; nowf = DateTime.Now) {
    Console.WriteLine(nowf.ToLongTimeString());
    System.Threading.Thread.Sleep(1000);
  }

  Console.WriteLine("Do While Time");
  DateTime noww = DateTime.Now, endTimew = noww.AddSeconds(5);
  do {
    Console.WriteLine(noww.ToLongTimeString());
    System.Threading.Thread.Sleep(1000);
    noww = DateTime.Now;
  } while (noww < endTimew);
}



Alan。


Alan.


你可以用'while'循环替换每个for,foreach或do / while循环但是相反的情况并非如此......

'for'循环正在循环以进行可量化的迭代次数:如果永远不满足退出条件,您的示例(do / while)可能会永远运行。 />


因此,如果没有关于您的条件的更多详细信息,那么答案是:不可能。
You can replace every for, foreach or do/while loops with 'while' loops but ythe opposite is not true...
'for' loops are looping for a quantifiable number of iterations : your example (do/while) might well run forever if exit condition is never met.

So without further details on your condition then the answer is : not possible.


您需要实现do..while循环for loop right ??



因此在for循环中,在第一次迭代中,处理代码块所需的条件是什么。



对于类似的东西,

You need to implement do..while loop with for loop right??

So in for loop, at the first iteration, what ever be the condition you need to process the code block.

For that something like,
for (int i=0; (i==0)?false:i<10; i++)
 {
     //operations
 }





为什么你需要for..loop?如果你有...在手???



why do you need for..loop? if you have do..while in hand??


这篇关于如何替换“while”循环“for”环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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