Sie sind auf Seite 1von 3

Programmers usually use the while loop whenever they want a statement to repeat an action while a condition exists,

similar to an if statement that repeats as long as the condition is true. A while loop evaluates an expression and executes the code in the body of the loop if the expression is true. If the condition evaluates to true, a statement or series of statements runs before looping back to evaluate the condition again. When the condition evaluates to false, the statement or series of statements is skipped and the loop ends. Using while loops can be very useful when you aren't sure of how many times you'll need to loop over a block of code. For example, the following code traces numbers to the Output panel: var i:Number = 0; while (i < 5) { trace(i); i++; }

A for loop is generally used to execute an instruction a specific number of times which is usually determined by the program or the user. This loop counts the number of times the instruction will be executed. This loop is normally used when the number of repetitions are known.

Example for(var i:Number = 0; i < 15; i++) { if(i == 5) { continue; } trace(i); }

However as programmers One first basic rule is that you should use the for loop when the number of iterations is known. For instance:

The loop iterates over the elements of a collection. The loop iterates over a range of values which is known in advance at execution time. The object provides a way to iterate over its elements with a for statements (e.g files).

While the For loop and While Loop are similar they have a lot of differences. For example a while loop checks for the condition first, hence it may not even enter into the loop, if the condition is false. Meanwhile in a For Loop a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement Below is a sample code examining the For Loop and While Loop in a program

Source Code using System; using System.Collections.Generic; using System.Text; namespace Loops { class LoopDifference { static void Main(string[] args) { const int MAX = 5; for (int i = 0; i < MAX; i++) { System.Console.WriteLine("For Loop : {0}", i); } int j = 0; while (j < MAX) { System.Console.WriteLine("While Loop : {0}", j); j++; } int k = 0; do } } }

In terms of choosing which one of the control structures is more robut it all depends on the situation at hand or what the programmer is trying to achieve. For example, the while loop is the more general one because its loop condition is more flexible and can be more complicated than

that of a for loop. The condition of a for loop is always the same and implicit in the construction. A for loop stops if there are no more elements in the collection to treat. For simple traversals or iterations over index ranges it is a good advice to use the for statement because it handles the iteration variable for you, so it is more secure than while where you have to handle the end of the iteration and the change of the iteration variable by yourself. The while loop can take every boolean expression as condition and permits therefore more complex end conditions. It is also the better choice if the iteration variable does not change evently by the same step or if there are more than one iteration variable. Even if you can handle more than one iteration variable in a for statement, the collections from which to choose the values must have the same number of elements.

Das könnte Ihnen auch gefallen