VisualBuilder
  Home > Csharp > Tutorials > Control Statements – Loops Statement - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Control Statements – Loops Statement
  << Prev: Control Statement: Selection Statement in C sharp Next: Methods >>

A loop is defined as the block of code which repeats itself for a given true condition. The following loops are available in c# programming constructs.



  1. While Loop

  2. Do… While Loop

  3. For Loop

  4. ForEach Loop


 


While Loop :


A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a Boolean value of true.


 


Syntax :


while (<Boolean expression >) {


//Execute block of code


}



  • The Boolean expression is evaluated before any code in the following block has executed.

  • When the Boolean expression evaluates to true, the statement will execute. Once the statement have executed, control returns to the beginning of the while loop to check the boolean expression again.

  • When the boolean expression evaluates to false , the while loop statements are skipped and execution begins after the closing brace of that block of code. Variable must be declared and initialized before entering the while loop, and must Increment or decrement the variable in the loop. So that the loop should be definite.


 


Do… While Loop :


A “Do… While” loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the “Do… While” loop is guaranteed to execute at least one time. This means that the “Do… While” loop will display whatever there in the statement or block at least once.


 


Syntax:


do {


// execute this statements or blocks at least once.


} while (<Boolean expression>);


 


For Loop :


The for loop is much like the while loop, but the initialization, evaluation and increment or decrement can be done in one go. The for loop executes until the condition or evaluated expression returns true.


 


Syntax:


for ( <initializer list>; <Boolean expression>; <iterator list>) {


// execute the statement if the Boolean expression return true


}



  • Initializer List:- This can be comma separated list of expressions. The expressions are evaluated only once during the lifetime of for loop.

  • Boolean Expression:- The for loop execution depend on this expression, it will executes until or unless this expression returns true.

  • Iterator List:- The iterator list contains comma separated counter and this is basically used Either to increment or decrement the variable that is initialized in the Beginning.


 


ForEach Loop:


A ForEach loop is used to iterate through the items in the list. The ForEach loop is used to operate over an arrays or collections such as ArrayList, which is under System.Collections namespace.


 


Syntax:


foreach (<datatype><itemname>in<list>) {


//statements


}


 


Note:-The foreach loop is used to iterate over collection. The datatype should be same as the elements contained in the list.


 


 


Example: Demonstration of while, do…while, for, foreach loop


 




 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Collections;


 


namespace cSHARPEXAMPLES


{


  public partial class Form3 : Form


    {


    public Form3()


        {


            InitializeComponent();


        }


  private void btnWhile_Click( object sender, EventArgs e)


    {


    // Starting While Loop


        int _tempValue = 0;


        lblWhile.Text = "\n" ;


    while (_tempValue < 10)


        {


            lblWhile.Text = lblWhile.Text + "Loop Value: " + _tempValue + "\n" ;


            _tempValue++;


        }


    }


  private void doWhile_Click( object sender, EventArgs e)


    { //Starting Do-while Example


    //Do -while execute atleast once


        int _tempValue = 0;


    do {


        MessageBox.Show( "This Executes at Least once even though the condition is false" );


        } while (_tempValue > 0);


    }


  private void btnFor_Click( object sender, EventArgs e)


    { //Starting for Example


        lblFor.Text = "\n" ;


    for ( int i = 0; i < 10; i++)


        {


        if (i%2 == 0)


                lblFor.Text = lblFor.Text + "Loop Value: " + i + "\n" ;


        }


    }


  private void btnForEach_Click( object sender, EventArgs e)


    { //Starting for Each Example


        ArrayList arrList = new ArrayList ();


        arrList.Add( "Vikrant" );


        arrList.Add( "Atul" );


        arrList.Add( "Mandeep" );


        arrList.Add( "Gautam" );


        arrList.Add( "Pardeep" );


        lblforEach.Text = "\n" ;


    foreach ( String arr in arrList)


        {


            lblforEach.Text = lblforEach.Text + arr + "\n" ;


        }


    }


  }


}


 


 


OutPut:


 


Clicking on all the Buttons:


 




 


  << Prev: Control Statement: Selection Statement in C sharp Next: Methods >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
VoIP Internettelefonie
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising