Core C#

For Loop Anatomy

The for loop is the most complex type of loop available in C#. Its statement consists of three parts: initialization, conditional, and update. To illustrate how and where each of these parts is used in the loop, we have given a relatively simple for loop and decomposed by replacing the parts with their functional equivalents.

The first loop is the model, while the rest are the functional equivalents of it. The loop begins with initializaion of the variable i to 5. The conditional checks that the value of i is less than 13 before it runs the next loop. Finally, the update increments the value of i at the end of the loop, just before the conditional checks it for the next loop.

Remove a part of the for loop statement, we simply leave it blankl; then we replace it with its functional equivalent outside the for statement to illustrate the functionality. In the second version of the loop, we remove the conditional and add in an if at the beginning of the loop with a break to get us out of the loop. In the third version, we remove the update and replace it with the equivalent increment at the end of the loop. In the fourth version, we remove the initialization and replace it with the equivalent initialization outside the loop; notice that we added brace outside this statement to ensure that the variable does not exist outside the local scope. In the fifth version, we make all three replacements at once.

All of these loops do exactly the same thing, as the output illustrates. The empty for loop statement for(;;) is simply an unconditional infinite loop by itself.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            // 1. A simple for loop counting from 5 to 12
            Console.Write("1. ");
            for (int i = 5; i < 13; ++i) {
                Console.Write(i + "  ");
            }
            Console.WriteLine();

            // 2. The same loop with the conditional pulled out
            Console.Write("2. ");
            for (int i = 5; ; ++i) {
                if (i >= 13) {
                    break;
                }
                Console.Write(i + "  ");
            }
            Console.WriteLine();

            // 3. The same loop with the update pulled out
            Console.Write("3. ");
            for (int i = 5; i < 13; ) {
                Console.Write(i + "  ");
                ++i;
            }
            Console.WriteLine();

            // 4. The same loop with the initialization pulled out
            Console.Write("4. ");
            {
                int i = 5;
                for (; i < 13; ++i) {
                    Console.Write(i + "  ");
                }
            }
            Console.WriteLine();

            // 5. The same loop totally pulled apart
            Console.Write("5. ");
            {
                int i = 5;
                for (;;) {
                    if (i >= 13) {
                        break;
                    }
                    Console.Write(i + "  ");
                    ++i;
                }
            }
            Console.WriteLine();
        }
    }
}
 

Output

1. 5  6  7  8  9  10  11  12
2. 5  6  7  8  9  10  11  12
3. 5  6  7  8  9  10  11  12
4. 5  6  7  8  9  10  11  12
5. 5  6  7  8  9  10  11  12
Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.