|
Free
computer Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
||||||
The Times Table Code |
|||||||
|
As you can see from our last lesson, we've set up five Integer variables - number1, number2, multiplier, answer and i. The next thing we did was to pass whatever is in our two Textboxes straight into the two variable, number1 and number2. The start number goes into textbox one, and the end number goes into textbox2. number1 = Val(TextBox1.Text) In the next line of code, we set a starting value for the multiplier variable: multiplier = 2 Then we had our two Loops, one inside the other. The first is the Do Loop: Do While multiplier < 3 multiplier = multiplier
+ 1 This Do loop is exactly the same as the one you met before. All is does is go round and round While the variable called multiplier is Less Than 3. The bit of code between Do and Loop just keeps adding one to whatever is in multiplier (incrementing the variable). The other thing the Do Loop does is to execute our second loop - the For Loop. The For Loop gets executed each time around the Do Loop. The For Loop was this: For i = number1 To number2 answer = i * multiplier Next i Remember: the number1 and number2 variables hold our numbers from the Textboxes. We set these to 1 and 10. So our first line of the For Loop is really this: For i = 1 To 10 We're saying, "Start a For Loop". Whatever is in the variable called number1, make that the starting number for the Loop. Put this value into the variable called i. The end of the Loop will come when the variable called i has the value 10. Stop looping when you reach this value. The next part of the code reads this: answer = i * multiplier This means, Put into the variable called answer the following sum: whatever is in the variable called i multiplied by whatever is in the variable called multiplier. (For a longer discussion on how to use the basic Maths symbols in VB .NET, see here: basic Maths symbols in VB .NET ) So that says, put into the variable answer the sum of i times multiplier. We're getting whatever is in i from the For loop. But we're getting whatever is in the variable called multiplier from the Do Loop. So Visual Basic will read the Do While line after noting that the number 2 has been put into the variable called multiplier. It will then check to see if the end of the loop condition has been met - multiplier is Less Than 3. As 2 is not Less Than 2, VB drops down to the next line. The next line is a For Loop - For i = 1 To 10. This entire loop will be executed, and the code inside the For loop worked out. So the For loop gets executed 10 times. Which means that our sum will get executed 10 times. When the end condition of the For loop is met, Visual Basic will exit the For Loop and drop down to the line of code below Next i. The code below Next i is this: multiplier = multiplier + 1 What was inside the variable called multiplier was the number 2, so 2 + 1 = 3. And three is the new number inside the variable multiplier. Then, Visual Basic drops down to the next line, which is Loop. So it goes back up to the Do While line of code Do While multiplier < 3 The same question is asked again: Have we met the end condition for the Do Loop? The end condition was "Keep looping while the variable called multiplier is Less Than 3". Because the value in multiplier is now equal to 3 and not less than 3, the end condition has been met. When the end condition is met, VB exists the Do loop.
No more reading these lessons online - get the eBook here!
ExerciseChange the Do While line to this: Do While multiplier < 5 Can you guess what will appear in your list
box? Run your programme and find out. Finally, a word about the line that displays your text in the list box. It was this: ListBox1.Items.Add(i & " Times " & multiplier & " = " & answer) To add items to a list box with code, first you type the name of your list box: ListBox1 Type a full stop and a drop down list will appear. Select Items from the list. ListBox1.Items Type another full stop and again a drop down list will appear. Select the Add Method ListBox1.Items.Add This method, not surprisingly, lets you add items to your list box. Whatever you want to add goes between a pair of round brackets: ListBox1.Items.Add( ) In between the round brackets, we have this for our code: i & " Times " & multiplier & " = " & answer It might be a bit long, but there are 5 parts to it, all joined together by the concatenate symbol (&): i The variable i holds the current value
of the For Loop; " Times "
is just direct text; multiplier holds the
value we're multiplying by (our times table); "
= " is again direct text; and answer
is the answer to our times table sum. ListBox1.Items.Clear() So instead of selecting Add from the final drop down list, select Clear. ExerciseAdd another textbox to your form. This will represent the "times table". So far you have been getting this value directly from the code. For this exercise, get the multiplier value directly from the textbox. Add appropriate labels to all your textboxes. The multiplier variable is the starting point for your "times tables". What else do you need to amend in the code? Think about what happens if you entered a 5 into your new Textbox. If you enter a number 5 or greater, the code doesn't get executed! Can you understand why that is? If not, I suggest you go over this looping section again.
|