HomeSearch

C# Do While Loop Example

Use the do-while loop. Examine and benchmark the syntax for this loop.

Do loop.

This keyword begins a loop. The loop body comes before its condition (which is specified in a while expression). Every looping construct has advantages.

This loop

is rarely needed—it probably has more disadvantages than advantages. We use the do-while loop (but we probably should not).

Example program.

First we use the do-while loop to sum the values of the elements in an int array. The array here is known to have 4 elements, so we can avoid checking its length.Int ArrayArray Length

Here: The code declares an integer array of four values. A do-while loop is executed, summing each value in the array.

Important: The length of the array is not checked before adding the first element's value to the sum.

Note: This code would throw an exception if the array was empty. The array would be accessed out-of-range.

IndexOutOfRangeException
C# program that uses do-while loop class Program { static void Main() { int[] ids = new int[] { 6, 7, 8, 10 }; // // Use do-while loop to sum numbers in 4-element array. // int sum = 0; int i = 0; do { sum += ids[i]; i++; } while (i < 4); System.Console.WriteLine(sum); } } Output 31

Notes, loops.

For the do-while loop, you just use the "do" keyword before the loop block curly brackets. After the brackets, you specify the condition while.

While: The while is executed after the code is run. No condition is tested before the statements in the loop are executed the first time.

Loops compared.

The most common type of loop (other than foreach) is the for-loop. It allows you to specify the 3 conditions right at the start.

Here: In these examples, we modify a for-loop. We turn it into a do-while loop. We see how the loops compare to each other.

For: This loop walks through the indexes 0, 1, 2, 3 and 4. The sum of these numbers is 10, which is the result of the SumFor method.

For

Do-while: This loop also walks through the indexes 0, 1, 2, 3 and 4. But it will evaluate the index 0 without checking any bounds.

While: The body statements of this loop are the same as in the do-while loop. It has the iteration statement at the start.

While
For-loop: C# static int SumFor() { // // Sum numbers 0 .. 4 // int sum = 0; for (int i = 0; i < 5; i++) { sum += i; } return sum; } Do-while loop: C# static int SumDoWhile() { // // Sum numbers 0 .. 4 // int sum = 0; int i = 0; do { sum += i; i++; } while (i < 5); return sum; } While-loop: C# static int SumWhile() { // // Sum numbers 0 .. 4 // int sum = 0; int i = 0; while (i < 5) { sum += i; i++; } return sum; }

Do-while, first iteration.

Consider this program: it starts its iteration variable (value) at a constant. And it continues until the constant value 6 is reached.

So: We know beforehand that the first iteration will always run. The do-while loop will avoid the first bounds check.

Note: This theoretically could be faster than another kind of loop, but in practice the do-while does not help.

C# program that runs first iteration in loop always using System; class Program { static void Main() { // We start at a constant, so we know the first iteration will always be run. // ... This reduces the number of checks by 1. int value = 1; do { value++; Console.WriteLine("DO WHILE: " + value); } while (value <= 5); } } Output DO WHILE: 2 DO WHILE: 3 DO WHILE: 4 DO WHILE: 5 DO WHILE: 6

A discussion.

The critical difference between the for loop and the do-while loop is that in do-while, no conditions are checked before entering the loop.

Also: The while-loop is the same as the do-while loop, but with the condition at the start.

Notes, first iteration.

This difference in the first iteration is important. All other loops have a test before the first iteration. Think about the first iteration carefully.

Caution: The do-while loop is much less common. This makes errors due to human factors more likely.

IL.

With for, there is an additional "br.s" statement, which "unconditionally transfers control" to somewhere else. Testing revealed no difference in performance between the loops.IL

So: It is likely that the JIT optimized the loops to the same machine instructions.

A summary.

It is reasonable to avoid do-while loops. They are less familiar to developers. They do not measurably change performance.

Loops compared.

The 3 loops had no performance difference, and the for-loop had shorter syntax. For most situations, using for-loops in all possible places is probably best.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.