C# While Loop vs For Loop?

17,084

Solution 1

is the only difference between the two that the for loop forces you to declare a new variable and also set the iteration value each loop cycle at the start?

The for loop forces you to nothing. You can omit any of the 3 elements. The smallest form is

for(;;)  // forever
{
  DoSomething();
}

But you can use the 3 elements to write more concise code:

for(initializer; loop-condition; update-expression)
{
  controlled-statements;
}

is equivalent to:

{
    initializer; 
    while(loop-condition)
    {
       controlled-statements;

    continue_target:  // goto here to emulate continue
       update-expression;
    }
}

Note the outer {} braces, in for(int i = 0; ...; ...) the i is local to the for-loop. A clear benefit.

But the major difference in usage is when you call continue; inside the loop, much easier to get the logic wrong in a while-loop than in a for-loop.

Solution 2

Yes, they're exactly the same in the background (in Assembly, that is).

Usually, it is more common to use the for loop when the number of repeats is known and we're not going to change our counter(index).

while loop also has the advantage of a sentinel loop which is easier for example when taking inputs from a user until something specific is given.

Solution 3

Fundamentally, the differences are: For loop knows in advance how many times it will loop, whereas a while loop doesn’t know. For loop has an initialization step whereas a while loop doesn’t For loop uses a “step value” or increment/decrement step, whereas a while loop doesn’t. Here is an example of a Java for loop looping from 0 to 99 and printing out the numbers. Notice how the loop is initialized and incremented as part of the for loop structure.

for(int i=0; i<100; i++) {
  System.out.println(i);
}

Here is an example of a Java while loop printing out all the elements in an integer array. Notice how the loop variable is initialized before the loop and is incremented inside the loop body.

int [] intArray = {1, 3, 5, 7, 9}; 
int i=0;
while(i<intArray.length) {
  System.out.println(intArray[i++]);

}

Share:
17,084
at541
Author by

at541

Updated on June 15, 2022

Comments

  • at541
    at541 almost 2 years

    In C# a question has been bugging me for a while and its what is that actual major difference between a While and For Loop. Is it just purely readability ie; everything you can essentially do in a for loop can be done in a while loop , just in different places. So take these examples:

    int num = 3;
    while (num < 10)
    {
        Console.WriteLine(num);
        num++;
    } 
    

    vs

    for (int x = 3; x < 10; x++)
    {
        Console.WriteLine(x);
    }
    

    Both code loops produce the same outcome and is the only difference between the two that the for loop forces you to declare a new variable and also set the iteration value each loop cycle at the start? Maybe I'm missing something else in terms of any major differences but it would good if someone can set me straight regarding this. Thanks.