at541
at541

Reputation: 257

C# While Loop vs For Loop?

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.

Upvotes: 8

Views: 26040

Answers (4)

Rohan Rao
Rohan Rao

Reputation: 2613

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.

Upvotes: 0

Arshman Saleem
Arshman Saleem

Reputation: 205

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

}

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273844

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.

Upvotes: 13

OmerM25
OmerM25

Reputation: 253

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.

Upvotes: 4

Related Questions