Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# For Loops: Syntax, Flow, and Practical Examples

Mastering C# For Loops

Learn how to use the for loop in C#, understand its execution flow, and see practical examples that cover common use cases and edge scenarios.


What Is a For Loop?

A for loop repeats a block of code a specified number of times. It’s the most common way to iterate when the number of repetitions is known or can be calculated.

Syntax

for (initialization; condition; iterator) {
    // loop body
}

How It Executes

  1. The initialization runs once, setting up loop variables.
  2. Next, the condition is evaluated. If it’s true, the loop body runs.
  3. After the body, the iterator executes, usually modifying the loop variable.
  4. The condition is checked again; the cycle repeats until the condition becomes false.
  5. If the condition is initially false, the loop body never runs.

Execution Flow Diagram

Mastering C# For Loops: Syntax, Flow, and Practical Examples

Practical Examples

Example 1: Basic Iteration

using System;

class ForLoopDemo {
    static void Main() {
        for (int i = 1; i <= 5; i++) {
            Console.WriteLine($\"C# For Loop: Iteration {i}\");
        }
    }
}
Output:
C# For Loop: Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5

Example 2: Sum of First N Natural Numbers

using System;

class SumDemo {
    static void Main() {
        int n = 5, sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        Console.WriteLine($\"Sum of first {n} natural numbers = {sum}\");
    }
}
Output:
Sum of first 5 natural numbers = 15

Example 3: Multiple Initialization and Iterator Expressions

using System;

class MultiExprDemo {
    static void Main() {
        for (int i = 0, j = 0; i + j <= 5; i++, j++) {
            Console.WriteLine($\"i = {i} and j = {j}\");
        }
    }
}
Output:
i = 0 and j = 0
i = 1 and j = 1
i = 2 and j = 2

Omitting Parts of the Loop

All three parts of a for statement are optional. A for loop with missing pieces behaves like a while loop.

Example 4: No Initialization or Iterator

using System;

class OmitPartsDemo {
    static void Main() {
        int i = 1;
        for (; i <= 5; ) {
            Console.WriteLine($\"C# For Loop: Iteration {i}\");
            i++;
        }
    }
}
Output:
C# For Loop: Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5

Infinite For Loops

When the condition never becomes false, the loop runs forever.

Example 5: Explicit Infinite Loop

for (int i = 1; i > 0; i++) {
    Console.WriteLine($\"C# For Loop: Iteration {i}\");
}

Or the classic:

for (;;) {
    // endless loop
}

Key Takeaways

C Language

  1. C# foreach Loops: Iterate Arrays and Collections with Ease
  2. Master C++ For Loops: A Step-by-Step Guide
  3. Mastering the C for Loop: Syntax, Mechanics, and Practical Examples
  4. Mastering Python For Loops: Syntax, Examples, and Advanced Patterns
  5. Master Java For Loops: Syntax, Examples, and Best Practices
  6. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  7. Master C Loops: For, While, and Do‑While Explained with Practical Examples
  8. Java Program to Identify Armstrong Numbers Using For Loop
  9. Mastering For Loops in Verilog: Build Reusable Hardware Logic
  10. IoT for Predictive Maintenance: Proactive Strategies to Cut Downtime and Costs