Search | Contact | Link To Us  

      ASP.NET      

ASP.NET
Dev Environment
Web Forms
Web Controls
ADO .NET
Web User Controls
Deployment
XML Web Services
ASP.NET Feedback Form
Resources
Discussions
VB .NET
About



Advertisement






































     HomeASP.NETWeb Forms> Loops 

Loops  

For Loop 

The For loop is the most popular loop. For loops enables us to execute a series of expressions multiple numbers of times. The For loop needs a loop index, which counts the number of loop iterations as the loop executes.

For Loop Sample

The following code demonstrates for loop.

Private Sub ForLoop_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles ForLoop.Click
'assuming you have a button named ForLoop
Dim i As Integer
For i = 1 To 5
Response.Write("Welcome to ASP.NET")
Next i
End Sub

Do Loop

The Do loop can be used to execute a fixed block of statements indefinite number of times. The Do loop keeps executing it's statements while or until the condition is true. Two keywords while and until can be used with the do loop. The Do loop also supports an Exit Do statement which makes the loop to exit at any moment.

Do Loop Sample

The following code demonstrates Do loop.

Private Sub DoWhile_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles DoWhile.Click
'assuming you have a button named DoWhile
Dim x As Integer
x = 0
Do Until x = 10
x = x + 1
Response.Write(x)
Loop
End Sub

If....Then....Else Statement

If conditional expression is one of the most useful control structures which allows us to execute a expression if a condition is true and execute a different expression if it is False. If the condition is true the statements following the Then keyword will be executed, else the statements following the ElseIf will be checked and if true, will be executed, else the statements in the else part will be executed.

If Then Sample

The following code demonstrates If..Then..Else conditional statement. It asks the user to enter a number between 1 and 3 in a textbox and checks for the number entered using the If..Then..Else statement and displays some text if the condition is true.

Private Sub IfThen_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles IfThen.Click
If CInt(TextBox1.Text) = 1 Then
Label1.Text = "You entered" & CInt(TextBox1.Text)
ElseIf CInt(TextBox1.Text) = 2 Then
Label1.Text = "You entered" & CInt(TextBox2.Text)
ElseIf CInt(TextBox1.Text) = 3 Then
Label1.Text = "You entered" & CInt(TextBox2.Text)
Else
Label1.Text = "You entered another number"
End If
End Sub

Select Case Statement

Select Case is similar to If..Then..Else statement. Select Case allows us to do various comparisions against one variable that we use throughout the whole statement. Select Case is cleaner and easier to understand if you are continually comparing against one variable.

Select Case Sample

The following sample asks the user to enter a vowel in a textbox and checks the value entered using the the Select Case statement and if true displays some text.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
Select Case TextBox1.Text
Case "a"
Label1.text = "You entered A"
Case "e"
Label1.Text = "You entered E"
Case "i"
Label1.Text = "You entered I"
Case "o"
Label1.Text = "You entered O"
Case "u"
Label1.Text = "You entered U"
End Select
End Sub

Live Code Demo


If...Then...Else

Enter a number between 1 and 3 

Select Case

Enter a vowel and click the button 


  Privacy Policy | Terms of Use | Site Map | Contact

  © 2004-2007 Startvbdotnet.com. All rights reserved.