For loop in C#

By   Calendar icon Mar 19, 2019   Level 1 icon

Description:

Here we explain what a for loop is in C# and we give some examples. We are also interested in nested loops. A for loop is used to repeat instructions.

Preferencesoft

Tags: C#

For loop

The for loop instruction to repeat a statement or sequence of statements until a given condition becomes false.

Most often, for loop is used to repeat a number of times a statement block.

The syntax of a for loop is as follows:

for (initialization; condition; updates) instruction;

Or

for (initialization; condition; updates) 
{
    instruction_1;
    // ...
    instruction_n;
}
  1. It executes initialization statement once, some variables are initialized
  2. tests the condition. If the result is false, then stops the loop otherwise it continues to the next step
  3. executes statements,
  4. then updates variables (incrementation, decrementation, ...)
  5. and goes back to step 2

The for-loop instruction has other variants that we will examine in the following paragraphs.

for (int i=0; i < n; i++) instruction;
for (int i=n; i>=0; i--) instruction;

In these two loops the variable i plays the role of counter.

Flow chart of a for loop

for loop flow chart

Examples

Example

Let us write a program that asks the user an integer n >= 0 and that displays n symbols aligned + and - starting with +

For example, for n = 5, we obtain this line:

+-+-+

using System;
namespace PlusMinus
{
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        System.Console.Write("Enter an integer n>=0 ");
        int n=int.Parse(System.Console.ReadLine());
        if (n>=0)
        {
            for (int i=0; i<n; i++)
                if (i%2 == 0) System.Console.Write("+");
                else System.Console.Write("-");
            System.Console.WriteLine();
        }
        System.Console.ReadKey();
    }
}
}

Example

Let us write a program that asks the user for an integer n >= 0 and displays all integers from that number down to 0.

using System;
namespace Decrease
{
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        System.Console.Write("Enter an integer n>=0 ");
        int n=int.Parse(System.Console.ReadLine());
        if (n>=0)
        {
            for (int i=n; i>=0; --i)
                System.Console.WriteLine(i);
        }
        System.Console.ReadKey();
    }
}
}

Example

Let us write a program that asks the user an integer n and that displays all the even integers from 0 up to 2n separated by a comma then all the odd integers from 1 to 2n + 1 separated by a comma.

using System;
namespace Even
{
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        System.Console.Write("Enter an integer n>=0 ");
        int n=int.Parse(System.Console.ReadLine());
        for (int i=0; i<n; i++)
            System.Console.Write("{0},",2*i);
        System.Console.WriteLine("{0}",2*n);
        System.Console.ReadKey();
    }
}
}
using System;
namespace Odd
{
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        System.Console.Write("Enter an integer n>=0 ");
        int n=int.Parse(System.Console.ReadLine());
        for (int i=0; i<n; i++)
            System.Console.Write("{0},",2*i+1);
        System.Console.WriteLine("{0}",2*n+1);
        System.Console.ReadKey();
    }
}
}

Nested for-loops

Two loops are said to be nested when one is inside the other, that is, when one of the two loops is placed in the instruction block of the other loop.

Example

Write a program that displays an isosceles triangle of height n stars according to the following model:

   *
  ***
 *****
*******

The base features 2n-1 stars.

using System;
namespace Triangle
{
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        System.Console.Write("Enter an integer n>=0 ");
        int n=int.Parse(System.Console.ReadLine());
        if (n>=0)
        {
            string str="";
            for (int i=0; i<n; i++)
            {
                for (int j=0; j<n-i-1; j++)
                    System.Console.Write(" ");
                for (int j=0; j<2*i+1; j++)
                    System.Console.Write("*");
                System.Console.WriteLine();
            }
        }
        System.Console.ReadKey();
    }
}
}

CSharp