Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to replace "while" loop by "for" loop in C#, how to do this? Thanks.
E.g:
C#
do
{
     // Do here
} while (condition);


convert it to "for" loop????
Posted
Updated 2-Aug-20 19:49pm

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.
C#
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
C#
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.
 
Share this answer
 
Comments
lukeer 24-Jul-13 8:25am    
Have a 5 for emphasizing the catholicity of "for".
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.
 
Share this answer
 
Comments
Andrewpeter 24-Jul-13 5:19am    
Thank a lot for your answer, I've understood it.
Phil J Pearson 24-Jul-13 6:41am    
"the answer is : not possible"
That's just wrong! Of course it's possible:

for (bool first = true; first || condition; first = false)
{
// do whatever
}
lukeer 24-Jul-13 8:22am    
I'd upvote if I could. Have a virtual 5 for bending the syntax to your will.
Guirec 24-Jul-13 23:44pm    
Vicious solution :)
You are right that is doing the job but I would still not consider that as a viable answer : apart if you want to appear in coding horror's hall of fame :)
Phil J Pearson 24-Jul-13 6:49am    
However, it would not be wise to supply this as your homework answer unless you understand it!
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,
C#
for (int i=0; (i==0)?false:i<10; i++)
 {
     //operations
 }


why do you need for..loop? if you have do..while in hand??
 
Share this answer
 
As Guirec Le Bars told you, for loops[^] are intended to loop for a countable number of iterations, but that doesn't mean they cannot be used otherwise. The following example will loop as long as condition equals true.
C#
for(;condition;)
{
    Whatever();
}
There are three parameters to the for loop. The example omits the first and last and that compiles well.
[Edit]Corrected parameter order[/Edit]

But Guirec Le Bars is right for another reason: The for loop checks condition before entering the loop. If it doesn't equal true, no single iteration is done.

do{ }while() on the other hand execute once and check afterwards, whether or not to repeat. So in spite of a condition that doesn't equal true, the loop is executed once.
 
Share this answer
 
v2
Its very simple in c# like c,c++ have
for(int i=0;i<=condition;i++)

Thanks
Prince Sharma
 
Share this answer
 
Comments
Andrewpeter 24-Jul-13 5:16am    
Oh, "Condition" is logical expression, can you compare an integer with a logical expression?
Guirec 24-Jul-13 5:23am    
no you can't... that answer is misleading

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900