Net-informations.com
SiteMap  | About    

How to use C# for loop

There are many situation when you need to execute a block of statements several number of times in your applications. The for loop in C# is useful for iterating over arrays and for sequential processing. This kind of for loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate. That is the statements within the code block of a for loop will execute a series of statements as long as a specific condition remains true.

Every for loops defines initializer, condition, and iterator sections.

Syntax:

for(initialization; condition; step) code statement

for-loop
initialization : Initialize the value of variable. condition : Evaluate the condition step : Step taken for each execution of loop body

The for loop initialize the value before the first step. Then checking the condition against the current value of variable and execute the loop statement and then perform the step taken for each execution of loop body.

int count = 4; for (int i = 1; i < = count; i++) { MessageBox.Show("Current value of i is - " + i); }

The initializer declares and initializes a local loop variable, i, that maintains a count of the iterations of the loop. The loop will execute four times because we set the condition i is less than or equal to count.

  for (int i = 1; i < = count; i++)

  initialization	: int i = 1
  Initialize the variable i as 1, that is when the loop starts the

value of i is set as 1

  condition		: i < = count
  Set the condition i < =count , that is the loop will execute up to

when the value of i < = 4 (four times)

  step			: i++

Set the step for each execution of loop block as i++ ( i = i +1)

The output of the code as follows :

Current value of i is - 1

Current value of i is - 2

Current value of i is - 3

Current value of i is - 4

C# Sample For loop program full source code

using System; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int count = 4; for (int i = 1; i < = count; i++) { MessageBox.Show("Current value of i is - " + i); } } } }

Infinite Loop

All of the expressions of the for loop statements are optional. A loop becomes infinite loop if a condition never becomes false. You can make an endless loop by leaving the conditional expression empty. The following statement is used to write an infinite loop.

for (; ; ) { // statements }

Here the loop will execute infinite times because there is no initialization , condition and steps.

break and continue

We can control for loop iteration with the break and continue statements. break terminates iteration and continue skips to the next iteration cycle. The following program shows a simple example to illustrate break and continue statement.




using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < = 5; i++)
            {
                if (i == 2) continue;
                if (i == 3) break;
                MessageBox.Show("execute " + i + " times !!");
            }
        }
    }
}

 CSharp Statements - Related Contents


 CSharp Statements - Related Programs