Difference between for(;;) and while (true) in C#? -
Syntactically I see that they have reached indefinitely until a break, but are they the same thing Compiled for? Is it a little faster because there is no condition to check it? Apart from code readability, is there any difference?
Looking at this input:
Private static zero forLoop ( ) {Int n = 0; For (;;) {console} WrightLine (n ++); }} Private static zero whileLoop () {int n = 0; While (true) {Console.WriteLine (n ++); }}
... You get this output:
.method private hidebysig static zero forLoop () cil managed {// code size 14 (0xe) .MaxStack 3 .Local 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 zero [MScorlib] system. Console :: LinLine (int32) IL_000c: br.s IL_0002} // End method of program: ForLoop .method Private hidebysig static zero while whileLoop () cil managed {// code size 14 (0xe) .MaxStack 3.Local Init ([0] int 32 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 zero [ Mscorlib] system Console :: LinLine (Int32) IL_000c: br.s IL_0002} // // Program of Program: WhileLoop
remarkably similar, I say (evenly, even ).
Comments
Post a Comment