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

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5