8

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.

5
  • 2
    "Is it just purely readability " - yes. Though the for loop is more idiomatic. Commented Feb 14, 2016 at 18:58
  • While loop doesnt include an sort of stepping in its arguments. Its also possible to set a while loop to have no definite end in the loop, while for loops have a defined beginning and end. Commented Feb 14, 2016 at 19:01
  • 1
    For loops don't have to have a defined end, @shaun. As an obvious counter-example, for (; ;) is an infinite loop. Commented Feb 14, 2016 at 19:03
  • Strongly related: stackoverflow.com/questions/552766/… Commented Feb 14, 2016 at 19:03
  • 4
    The for/while/do keywords are all just syntax sugar for goto :) Commented Feb 14, 2016 at 19:04

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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++]);

}

Comments

0

When you are sure what will be the end value or how much the loop should execute(until what value), use 'for' loop, otherwise use 'while' loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.