Difference between for (;;) and while (true) in C #?

Syntactically, I see that they loop endlessly until the break statement is reached, but are they compiled to the same thing? Is it a little faster because it does not have a condition to check? Besides code readability, is there any difference?

+17
c #
Jul 27 '10 at 20:03
source share
7 answers

Given this input:

private static void ForLoop() { int n = 0; for (; ; ) { Console.WriteLine(n++); } } private static void WhileLoop() { int n = 0; while (true) { Console.WriteLine(n++); } } 

... you get this output:

 .method private hidebysig static void ForLoop() cil managed { // Code size 14 (0xe) .maxstack 3 .locals init ([0] int32 n) IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: ldloc.0 IL_0003: dup IL_0004: ldc.i4.1 IL_0005: add IL_0006: stloc.0 IL_0007: call void [mscorlib]System.Console::WriteLine(int32) IL_000c: br.s IL_0002 } // end of method Program::ForLoop .method private hidebysig static void WhileLoop() cil managed { // Code size 14 (0xe) .maxstack 3 .locals init ([0] int32 n) IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: ldloc.0 IL_0003: dup IL_0004: ldc.i4.1 IL_0005: add IL_0006: stloc.0 IL_0007: call void [mscorlib]System.Console::WriteLine(int32) IL_000c: br.s IL_0002 } // end of method Program::WhileLoop 

Remarkably similar, I would say (identical, even).

+36
Jul 27 '10 at 20:09
source share

There is absolutely nothing in modern compilers.

Historically, however, for(;;) was implemented as a single leap, while while(true) also checked true.

I prefer while(true) as it makes it clearer what I am doing.

+21
Jul 27 2018-10-10T00:
source share

I have not studied the output code, but there should be no difference. Any worthy compiler will do a fairly simple loop optimization to see that this condition is a constant expression and therefore does not need to check every iteration.

If someone is faster than the other, the C # compiler developers need something “for them” ...

+2
Jul 27 2018-10-10T00:
source share

If I could, I would suggest you consider a slightly different issue. If you use any of them often enough to take care, you are likely to structure your code poorly. Although there are things like embedded systems that really run forever, there are no loops in most normal codes. Writing a loop that claims to run forever usually means that you have hidden the exit condition for the loop somewhere inside, with some other control flow (for example, if (whatever) break; ) as a real exit from the loop.

This can and should usually be avoided. Although there are situations where break statements make sense, they usually need to handle unusual situations, rather than writing a loop that says one thing and does the other (that is, it says “run forever,” but actually “works until until the condition is MET ").

+1
Jul 27 '10 at 20:18
source share

They compile to the same thing. You can write a test application that implements both methods, and then use ILDASM to verify that the IL is identical.

0
Jul 27 '10 at 20:11
source share

Debug build matches to (true). Use a reflector and you can see the results.

  static void Main(string[] args) { ExecuteWhile(); ExecuteFor(); } private static void ExecuteFor() { for (; ; ) { Console.WriteLine("for"); string val = Console.ReadLine(); if (string.IsNullOrEmpty(val)) { Console.WriteLine("Exit for."); break; } } } private static void ExecuteWhile() { while (true) { Console.WriteLine("while"); string val = Console.ReadLine(); if (string.IsNullOrEmpty(val)) { Console.WriteLine("Exit while."); break; } } } 

Testing the ExecuteFor method in the reflector.

 private static void ExecuteFor() { while (true) { Console.WriteLine("for"); if (string.IsNullOrEmpty(Console.ReadLine())) { Console.WriteLine("Exit for."); return; } } } 

An optimized version of the same code gives different results for ExecuteFor

 private static void ExecuteFor() { do { Console.WriteLine("for"); } while (!string.IsNullOrEmpty(Console.ReadLine())); Console.WriteLine("Exit for."); } 

For details, here is the optimized ExecuteWhile ...

 private static void ExecuteWhile() { do { Console.WriteLine("while"); } while (!string.IsNullOrEmpty(Console.ReadLine())); Console.WriteLine("Exit while."); } 
0
Jul 27 2018-10-10T00:
source share

As others have already mentioned, it should have absolutely no difference with any modern compiler.

I did a short test on my computer, selecting a couple of operations using one or the other, and they always took almost the same time. Of course, due to other processes, these tests are not 100% accurate, but if there is a difference in speed (should not be), then this is microscopic.

0
Jul 27 2018-10-10T00:
source share



All Articles