Iteration Statements


  You can create loops by using the iteration statements.  When execution leaves a scope(loop), all automatic objects that were created in that scope are destroyed.

The following keywords are used in iteration statements:

1). For
2). Foreach
3). While
4). Do While


1).For :-
     For one of the iterate statement, It will iterate in between the values. if you know number of iterations will be there that time go for for loop.

Syntax:-

public void Main()
{
    for (int i = 0; i< 10; i++)
    {
        Console.WriteLine("i value : "+ i);
    }
}


for(initialization;condition;increment/decrement)
{
}
initialization:-
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Example
<pre class="brush: csharp">// Comment
public class Testing {
public Testing() {
}

public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}
</pre>
2).Foreach :-
    Foreach used mainly collections,arrays etc, It can be used when we don't know how many iterations it will take that time we go for Foreach.

Syntax:-
foreach(var variable in colletionname)
{
}

 if user wants to iterate each row in class DataTable.

DataTable dt=new DataTable();
//you do not know how many rows in a Table then Go for  foreach.

 foreach(DataRow row in dt.Rows)
 {
     string col1=row["Col1"].Tostring();
 }
    
    

Now you Can run your application

1 Comments: