C# For Loop

C# For Loop C#-For-Loop

Unlike other programming languages [C#,Vb.net] also supports for—loop. Here we will see syntax, example of break and continue looping scenarios. (VB.net)

C# for—loop syntax

for—loop syntax using c# programming language.

for (; ; )
{
// Your code/operations goes here.
}
Example:
for(int i=0; i<=10; i++) 
{
// operation on collection goes here. 
} 
for ( Statement; condition ; statement ) 
{ 
// Your code/operations goes here. 
}

 

Declare the counter before first semi colon inside the for statement brace, You can even declare the variable outside of the for statement. The condition has Boolean value either true or false, based on condition for loop continue or breaks.

C# for loop example

Some of you must be knowing that the string is a representation of array of characters. In this example I will use C#.net to iterate each character of string using for—loop.

using System;
namespace Csharp.ForLoop.Example
{
class Program
{
static void Main(string[] args)
{
string arrayOfCharacter = "StringIsAnArrayOfCharacter";
for (int i = 0; i < arrayOfCharacter.Length; i++)
{
Console.WriteLine(arrayOfCharacter[i]);
}
Console.ReadLine();
}
}
}
Output
S
t
r
i
n
g
I
s
A
n
A
r
r
a
y
O
f
C
h
a
r
a
c
t
e
r
C#-For-Loop-break

c# for loop break

Break is often used to stop looping further after specified condition. Below example breaks the loop if an array element 'Satalaj' is found.

using System;
namespace Csharp.ForLoop.Example
{
class Program
{
static void Main(string[] args)
{
string[] arrOfStrings = new string[] { "More", "Satalaj", "ASP.net", "MVP" };
for (int i = 0; i < arrOfStrings.Length; i++)
{
if (arrOfStrings[i] == "ASP.net")
break;
Console.WriteLine(arrOfStrings[i]);
}
Console.ReadLine();
}
}
}
Output
More
Satalaj
VB.net-Program

(VB.net) break example

Namespace Csharp.ForLoop.Example
Class Program
Private Shared Sub Main(args As String())
Dim arrOfStrings As String() = New String() {"More", "Satalaj", "ASP.net", "MVP"}
For i As Integer = 0 To arrOfStrings.Length - 1
If arrOfStrings(i) = "ASP.net" Then
Exit For
End If
Console.WriteLine(arrOfStrings(i))
Next
Console.ReadLine()
End Sub
End Class
End Namespace

Description: As you can see in the examples above the program looping breaks at occurrence break; statement. You can save time by using such conditional break inside loop.

C#-For-Loop-continue

(c#) for loop continue

In C# for loop the continue statement skips the current iteration after the occurrence of continue; statement. It doesn't stop entire loop execution. Let's see code below.

using System;
namespace Csharp.ForLoop.Example
{
class Program
{
static void Main(string[] args)
{
string[] arrOfStrings = new string[] { "More", "Satalaj", "ASP.net", "MVP" };
for (int i = 0; i < arrOfStrings.Length; i++)
{
if (arrOfStrings[i] == "ASP.net")
continue;
Console.WriteLine(arrOfStrings[i]);
}
Console.ReadLine();
}
}
}
More
Satalaj
MVP

(VB.net) for loop continue

Namespace Csharp.ForLoop.Example
Class Program
Private Shared Sub Main(args As String())
Dim arrOfStrings As String() = New String() {"More", "Satalaj", "ASP.net", "MVP"}
For i As Integer = 0 To arrOfStrings.Length - 1
If arrOfStrings(i) = "ASP.net" Then
Continue For
End If
Console.WriteLine(arrOfStrings(i))
Next
Console.ReadLine()
End Sub
End Class
End Namespace
More
Satalaj
MVP

Tags:

C# For LoopC# looping through arrayC# while loopC# Do While Loop ExampleJavaScript For LoopC# Break StatementC# StopWatchc# stringc# arraylistC# DateTime DifferenceC# XML XmlReader

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.