Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for and while loop in c#

Tags:

for (i=0 ; i<=10; i++) {     ..     .. }  i=0; while(i<=10) {     ..     ..     i++; } 

In for and while loop, which one is better, performance wise?

like image 275
Ashutosh Singh-MVP SharePoint Avatar asked Feb 16 '09 09:02

Ashutosh Singh-MVP SharePoint


People also ask

What is for loop and while loop in C?

For Loop and While Loop is entry-controlled loops. Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false.

What is the difference between for loop & while loop?

For is entry controlled loop. While is also entry controlled loop. used to obtain the result only when number of iterations is known.

What is while loop in C syntax?

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

What is while loop in C with example?

Example 1: while loopWhen i = 1 , the test expression i <= 5 is true. Hence, the body of the while loop is executed. This prints 1 on the screen and the value of i is increased to 2 . Now, i = 2 , the test expression i <= 5 is again true.


2 Answers

(update) Actually - there is one scenario where the for construct is more efficient; looping on an array. The compiler/JIT has optimisations for this scenario as long as you use arr.Length in the condition:

for(int i = 0 ; i < arr.Length ; i++) {     Console.WriteLine(arr[i]); // skips bounds check } 

In this very specific case, it skips the bounds checking, as it already knows that it will never be out of bounds. Interestingly, if you "hoist" arr.Length to try to optimize it manually, you prevent this from happening:

int len = arr.Length; for(int i = 0 ; i < len ; i++) {     Console.WriteLine(arr[i]); // performs bounds check } 

However, with other containers (List<T> etc), hoisting is fairly reasonable as a manual micro-optimisation.

(end update)


Neither; a for loop is evaluated as a while loop under the hood anyway.

For example 12.3.3.9 of ECMA 334 (definite assignment) dictates that a for loop:

for ( for-initializer ; for-condition ; for-iterator ) embedded-statement 

is essentially equivalent (from a Definite assignment perspective (not quite the same as saying "the compiler must generate this IL")) as:

{     for-initializer ;     while ( for-condition ) {         embedded-statement ;         LLoop:         for-iterator ;     } } 

with continue statements that target the for statement being translated to goto statements targeting the label LLoop. If the for-condition is omitted from the for statement, then evaluation of definite assignment proceeds as if for-condition were replaced with true in the above expansion.

Now, this doesn't mean that the compiler has to do exactly the same thing, but in reality it pretty much does...

like image 113
Marc Gravell Avatar answered Sep 18 '22 13:09

Marc Gravell


I would say they are the same and you should never do such micro-optimizations anyway.

like image 24
Alex Reitbort Avatar answered Sep 18 '22 13:09

Alex Reitbort