For My Next Trick - Loops


For My Next Trick – Loops

Other than helping us make decisions, computers can also save us from doing repetitive jobs. For example, if a student is late for school, they will probably be punished, for example, by writing "I will not be late for school" one hundred times for everyone to see.

Instead writing of these sentences themselves, the student may ask the computer for help by just telling the computer the sentence and number of times it has to be written. Then, they can just sit down, click a mouse button and let the computer do it for them; printing the meaningless sentence for a hundred times. The teacher didn't say anything about writing the sentence on the chalkboard!

Try It Out—The For ... Next Loop

Imagine there are a boy and girl, the boy loves the girl very much and would like to marry her. However, in order to show he is sincere, the boy has to write "I love you!" one hundred times. What should he do? In his place, I would turn on my laptop and write a program immediately, as the computer will finish this great task in a couple of seconds without pain.

Let's see how to write this program to complete this task.

  1. Create a new page in Web Matrix named MarryMe.aspx and start by placing an ImageButton control on the page.

  2. Add a blank line under the ImageButton and place a Label control on the page.

  3. Erase the Text property of the Label control and change the ImageUrl property of the ImageButton to Images/Heart.gif.

    You can find the Heart.gif image in a subfolder named Images as part of the code download. In Chapter 11, we explain in more detail about linking to subfolders but for now, all you need to do is create an Images subfolder in the current directory you're working in, and place your image in there, specifying the appropriate file name, of course.

  4. Let's write some code for this romantic program. Double-click on the ImageButton to add a ImageButton1_Click event handler and enter the following code:

    Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)  Dim Counter as Integer  For Counter = 1 To 100  ' Start looping 100 times  Label1.Text &= Counter.ToString() & ": I  Love You! " & "<BR />"  Next End Sub

    Important

    The &= operator concatenates (sticks together) a String expression to a String variable and assigns the result to the variable. The ToString() method will convert the numeric value of an instance to its equivalent string representation.

  5. That's it. Run the page and try clicking on the ImageButton. You should see something like the following:

How It Works

When you click on the ImageButton, ASP.NET calls the code in the ImageButton1_Click event handler. This is just like what a Button control does after the user clicks on it.

Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)

The first line of code declares an Integer object called Counter:

  Dim Counter as Integer

Visual Basic .NET will track the number of times the loop has been completed in the Counter variable as the program runs.

The loop starts iterating with the variable assigned to it (in this case Counter) with the initial value equal to the first number before the To in the For loop statement (in this case, 1), and continues until the Counter variable reaches the value of the number after the To (in this example, 100). With each iteration of the loop, the Counter variable is incremented by one, so in this case, the loop will run 100 times:

  For Counter = 1 To 100  ' Start looping 100 times     Label1.Text &=  Counter.ToString() & ": I  Love You! " & "<BR />"   Next

Each time it goes around, we append the value of Counter and the I Love You message
to Label1.

In this example, the Counter increases in increments of 1, which is the default. We can change this if we want by using the Step keyword:

  For Counter = 1 To 100 Step 5  ' Start looping 20 times     Label1.Text &= Counter.ToString() & ": I Love You!" & "<BR />"   Next




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net