Sometimes loop condition should be checked in the middle of the loop. The former is arguably more elegant than the latter:
Note: In nested loops and/or
switch must use more than just a simple
In addition to
break, there is also the keyword
continue. Instead of breaking completely the loop, it will simply skip the current iteration. It could be useful if you don't want some code to be executed if a particular value is set.
Here's a simple example:
Will result in:
Continue is often most useful in while or do-while loops. For-loops, with well-defined exit conditions, may not benefit as much.
Do - While Loop
It is similar to a
while loop, except that it tests the condition at the end of the loop body. The Do - While loop executes the loop once irrespective of whether the condition is true or not.
A For Loop is great for doing things a certain amount of time. It's like a While Loop but the increment is included with the condition.
A For Loop is set up like this:
Initialization - Makes a new local variable that can only be used in the loop.
Condition - The loop only runs when the condition is true.
Increment - How the variable changes every time the loop runs.
You can also leave out spaces in the For Loop, but you have to have all semicolons for it to function.
Output for 3:
foreach will iterate over any object of a class that implements
IEnumerable (take note that
IEnumerable<T> inherits from it). Such objects include some built-in ones, but not limit to:
T (arrays of any type),
Dictionary<TKey, TSource>, as well as interfaces like
- The type
ItemTypedoes not need to match the precise type of the items, it just needs to be assignable from the type of the items
- Instead of
varcan be used which will infer the items type from the enumerableObject by inspecting the generic argument of the
- The statement can be a block, a single statement or even an empty statement (
enumerableObjectis not implementing
IEnumerable, the code will not compile
- During each iteration the current item is cast to
ItemType(even if this is not specified but compiler-inferred via
var) and if the item cannot be cast an
InvalidCastExceptionwill be thrown.
Consider this example:
is equivalent to:
The most trivial loop type. Only drawback is there is no intrinsic clue to know where you are in the loop.
while, but the condition is evaluated at the end of the loop instead of the beginning. This results in executing the loops at least once.
Another trivial loop style. While looping an index (
i) gets increased and you can use it. It is usually used for handling arrays.
Modernized way of looping through
IEnumarable objects. Good thing that you don't have to think about the index of the item or the item count of the list.
While the other styles are used for selecting or updating the elements in collections, this style is usually used for calling a method straight away for all elements in a collection.
It is important to note that this method in only available on
List<T> instances and as a static method on
Array - it is not part of Linq.
Linq Parallel Foreach
Just like Linq Foreach, except this one does the job in a parallel manner. Meaning that all the items in the collection will run the given action at the same time, simultaneously.
IEnumerators can be iterated with a while loop:
New York Metro