Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Step Parameter & Nested For … To … Next loops

Similar presentations


Presentation on theme: "The Step Parameter & Nested For … To … Next loops"— Presentation transcript:

1 The Step Parameter & Nested For … To … Next loops
5.2 Iteration Loops The Step Parameter & Nested For … To … Next loops 18/07/2019

2 Learning Objectives State what the step parameter is used for.
State the general form of a nested For … To … Next iteration loop. State how to: Keep the current text and add new text to a control. To enter or start a new line. To enter spaces. 18/07/2019

3 For … To … Next e.g. Display the numbers from 1, 3, 5, 7, 9 Note:
Dim Number As Integer For Number = 1 To 10 Step 2 lblNumber.Text = Number Next Number Note: This loop will not display 11 as it is bigger than the 10 allowed 18/07/2019

4 A nested For … To … Next Iteration Loop
When you have one loop inside another. Think of the outer loop as a large cog driving a smaller cog which is the inner loop. Every time the larger cog revolves once (one repetition of the outer loop), the inner cog usually revolves more than once. As a solid real life example think of the second and minute hand. The minute hand would be the outer loop. The second hand would be the inner loop. 18/07/2019

5 General Form of a nested For … To … Next Iteration Loop
For (variable identifier = start value) To (end value) (Outer loop body statements) … (Inner loop body statements) … Next (variable identifier) O u t e r L o p I n e r L o p 18/07/2019

6 A nested For … To … Next Iteration Loop
lstNumber.Items.Clear() Dim OuterNumber As Integer Dim InnerNumber As Integer For OuterNumber = 1 To 4 lstNumber.Items.Add(“OuterNumber variable is ” & OuterNumber) For InnerNumber = 1 To 2 lstNumber.Items.Add(“InnerNumber variable is ” & InnerNumber) Next InnerNumber Next OuterNumber 18/07/2019

7 A nested For … To … Next Iteration Loop
The previous slide’s code will produce: OuterNumber variable is 1 InnerNumber variable is 1 InnerNumber variable is 2 OuterNumber variable is 2 OuterNumber variable is 3 OuterNumber variable is 4 18/07/2019

8 Note: To keep the current text and add new text to a control:
lblTable.Text = lblTable.Text & … (new text) To enter or start a new line: vbNewLine To enter spaces: Space(number) 18/07/2019

9 Program 5.2 Addition Table
Specification: Write a program to display the sum of row and column numbers. 18/07/2019

10 Program 5.2 Addition Table
Create a new project named ‘Addition Table’. Change the form’s Text property to ‘Addition Table’. 18/07/2019

11 Addition Table One large label.
To allow the enlargement of a label you need to turn the “AutoSize” property to False and then resize the label to be as large as you want. 18/07/2019

12 Program 5.2 Addition Table
Name the label lblTable. 18/07/2019

13 Program 5.2 Addition Table
Form1 code: Const Max = 5 Dim ColNumber As Integer Dim RowNumber As Integer Dim Sum As Integer 'Display '+' and '12' spaces in top right corner to start table. lblTable.Text = "+" & Space(12) For ColNumber = 0 To Max 'Simple For ... To ... Next loop 'Enter column numbers with 8 spaces in between. 'Note that 'lblTable.Text' keeps current text and '&' adds new text. lblTable.Text = lblTable.Text & ColNumber & Space(8) Next ColNumber 18/07/2019 Continued on next slide.

14 Program 5.2 Addition Table
'Enter a empty line. lblTable.Text = lblTable.Text & vbNewLine For RowNumber = 0 To Max 'Start outer loop. 'Start a new row. 'Enter first number in the row. lblTable.Text = lblTable.Text & RowNumber & Space(12) For ColNumber = 0 To Max 'Start of inner loop. Sum = ColNumber + RowNumber 'Enter addition lblTable.Text = lblTable.Text & Sum & Space(8) Next ColNumber 'End of inner loop. Next RowNumber 'End of outer loop. 18/07/2019

15 Program 5.1 Multiplication Table
Run the program and test it. Save and publish the program. 18/07/2019

16 Extension “Multiplication” Program 1
Write a program to produce the following multiplication table. Note you will have a problem with columns due to numbers >99. Use Courier New font (as this a fixed width font which means each character has the same width). Use an If …Then … End If statement to add less spaces when necessary. Please note that I actually don’t expect you to fully solve this. If you can then great otherwise spend around 15 minutes and then just send me what you have. 18/07/2019

17 The Step Parameter Used to change the variable identifier in a For … To … Next iteration loop by something other than +1. For (variable identifier = start value) To (end value) Step (increment value) (Loop Body statements) … Next (variable identifier) 18/07/2019

18 Extension “Between Two Numbers” Program 2
Extend the “Between Two Numbers” Program from 5.1 Loops so that it has a new button which will count down from a higher first number to a lower second number. Hints: Copy and paste the code from the original button into the code template of the new Count Down button and adjust as necessary. To count down write Step -1 at the end of the For … To … Step -1 Extension: Show only numbers between the two values not the values themselves. Stop the user entering letters. What happens if the second number is higher than the first number when this new second button is clicked? The loop does not end and results in what is called an infinite loop, resulting in the program freezing. The new “second” button should not allow the second number to be higher than the first number. 18/07/2019

19 Plenary What is the step parameter used for?
Used to increase the variable identifier in a For … To … Next loop by more than 1. 18/07/2019

20 Plenary What is the general form of a nested For … To … Next loop?
For (variable identifier = start value) To (end value) (Outer loop body statements) … (Inner loop body statements) … Next (variable identifier) O u t e r L o p I n e r L o p 18/07/2019

21 Plenary How do you: Keep the current text and add new text to a control. To enter or start a new line. To enter spaces. 18/07/2019

22 Plenary To keep the current text and add new text to a control:
lblTable.Text = lblTable.Text & … (new text) To enter or start a new line: vbNewLine To enter spaces: Space(number) 18/07/2019


Download ppt "The Step Parameter & Nested For … To … Next loops"

Similar presentations


Ads by Google