connectseekers.com

Thursday, 22 May 2014

Example of For Loop in C#.Net | Syntax of For Loop In C#.Net and VB.Net

5/22/2014 - By Pranav Singh 0

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.


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


in case of vb.net

            For i As Integer = 0 To objstudent.Count - 1
                Console.WriteLine("Name :" & objstudent(i).Name)
            Next

Now run the application for output: 

 



Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top