asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Programming Languages Primer > For Loops

ASP.NET Programming Languages Primer


The for statement ( For...Next in VB ) executes embedded statements a specified number of times.

for ( counter; condition; increment ) {
   ... statements ...
}

  C# VB JScript

Key Points

  • In C# and JScript, the for statement specifies a counter variable, a test condition, and an action that updates the counter.
  • The condition can be any valid expression that evaluates to true or false.
  • In VB, the for...next statement specifies a lower bound, an upper bound, and an optional step value. The counter variable must be initialized before the loop.

NOTE: The variable of a for statement must be of a type that supports the greater than ( > ), less than ( < ), and addition ( + ) operators; otherwise, a compile-time error occurs.

A for statement is executed as follows:

  • The counter variable is initialized once ( ex. int i=1; ).
  • The condition ( ex. i < 7; ) is evaluated.
  • If the condition is true, the embedded statements are executed, and the counter variable is updated using the increment expression ( ex. i ++ ).
  • Thereafter, control is passed back to the beginning of the for statement, and the processs is repeated as long as the condition is true.
  • When the condition returns false, the loop ends and control is passed to the statement after the for block.

The below example demonstrates use of a for loop to repeatedly render an HTML block, increasing the font size with each pass thru the loop.

 Show me 

Below are links to further code examples that illustrate using the for loop in ASP.NET web forms.

DataGrid CellPadding Property Example
Run Sample | View Source
Generating Table Rows and Cells Dynamically
Run Sample | View Source
WebControl BorderWidth Property Example
Run Sample | View Source
More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note