For loop is used for executing a statement again and again
for repeating the process again and again until the provided condition becomes false.
In this article I will give you syntax as well as with an example to how you
can use for loop in you c# or in your vb.net application.
Some of my previous articles as are follows : Event
Handling in windows programming using C# | How To Add Events at run time in
windows application controls using C#.Net and VB.Net , Syntax
For Switch In C#.Net and VB.Net , Merge
two datatables in C#.Net and VB.Net | How to merge multiple datatables using
C#.Net , Random
Number Generator C# .
Syntax of FOR LOOP:
C#.Net
for (initializer; condition; iterator)
{
Body
}
|
VB.Net
For initializer = condition
Body
Next
|
Example:
C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
namespace ProjectDemo
{
class Program
{
//example
of for loop in c#.net
static
void Main(string[]
args)
{
List<Student> objstudent = new List<Student>();
objstudent = StudentData();//Here you can pass your own data base collection
Console.WriteLine("<Example of for loop in C#.Net>");
for
(int i = 0; i < objstudent.Count;i++ )
{
Console.WriteLine("Name :" + objstudent[i].Name);
}
Console.ReadLine();
}
//Data to
our list...
public
static List<Student> StudentData()
{
List<Student> _objStudent = new List<Student>();
_objStudent.Add(new Student {
Name = "Ram", RollNo = 1, Marks
= 85.5 });
_objStudent.Add(new Student {
Name = "vinay", RollNo = 2,
Marks = 95.5 });
_objStudent.Add(new Student {
Name = "pranac", RollNo = 3,
Marks = 75.5 });
return
_objStudent;
}
}
public class Student
{
public
string Name { get;
set; }
public
int RollNo { get;
set; }
public
double Marks { get;
set; }
}
}
|
In above code for loop code is
for
(int i = 0; i < objstudent.Count;i++ )
{
Console.WriteLine("Name :" + objstudent[i].Name);
}
|
VB.Net
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.Data
Namespace ProjectDemo
Class Program
'example of
for loop in c#.net
Private
Shared Sub
Main(ByVal args As
String())
Dim
objstudent As New
List(Of Student)()
objstudent = StudentData()
'Here
you can pass your own data base collection
Console.WriteLine("<Example of for loop in VB.Net>")
For
i As Integer
= 0 To objstudent.Count - 1
Console.WriteLine("Name :" & objstudent(i).Name)
Next
Console.ReadLine()
End Sub
'Data to
our list...
Public
Shared Function
StudentData() As List(Of Student)
Dim
_objStudent As New
List(Of Student)()
_objStudent.Add(New Student() With
{ _
Key .Name = "Ram", _
Key .RollNo = 1, _
Key .Marks = 85.5 _
})
_objStudent.Add(New Student() With
{ _
Key .Name = "vinay", _
Key .RollNo = 2, _
Key .Marks = 95.5 _
})
_objStudent.Add(New Student() With
{ _
Key .Name = "pranac", _
Key .RollNo = 3, _
Key .Marks = 75.5 _
})
Return
_objStudent
End Function
End Class
Public Class Student
Public
Property Name() As
String
Get
Return
m_Name
End
Get
Set(ByVal value As String)
m_Name = Value
End
Set
End Property
Private
m_Name As String
Public
Property RollNo() As
Integer
Get
Return
m_RollNo
End
Get
Set(ByVal value As Integer)
m_RollNo = Value
End
Set
End Property
Private
m_RollNo As Integer
Public
Property Marks() As
Double
Get
Return
m_Marks
End
Get
Set(ByVal value As Double)
m_Marks = Value
End
Set
End Property
Private
m_Marks As Double
End Class
End Namespace
|
For
i As Integer
= 0 To objstudent.Count - 1
Console.WriteLine("Name :" & objstudent(i).Name)
Next
|
Now run the application for output:
0 comments:
Please let me know your view