3 2 Iteration Loops Nested Loops The Step

  • Slides: 21
Download presentation
3. 2 Iteration Loops Nested Loops & The Step Parameter 1/18/2022 1

3. 2 Iteration Loops Nested Loops & The Step Parameter 1/18/2022 1

Learning Objectives State what the step parameter is used for. State the general form

Learning Objectives State what the step parameter is used for. State the general form of a nested For … To … Next iteration loop. How to output to the same line rather than always to a new line. How to start an empty new line. State how to enter spaces. Explain what constants are and their advantages. 1/18/2022 2

For … To … Next e. g. Display the numbers from 1, 3, 5,

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

A nested For … To … Next Iteration Loop When you have one loop

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. n n 1/18/2022 The minute hand would be the outer loop. The second hand would be the inner loop. 4

General Form of a nested For … To … Next Iteration Loop O u

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

A nested For … To … Next Iteration Loop Dim Outer. Number As Integer

A nested For … To … Next Iteration Loop Dim Outer. Number As Integer Dim Inner. Number As Integer For Outer. Number = 1 To 4 n n Console. Write. Line(“Outer. Number variable is ” & Outer. Number) For Inner. Number = 1 To 2 Console. Write. Line(“Inner. Number variable is ” & Inner. Number) n Next Inner. Number Next Outer. Number 1/18/2022 6

A nested For … To … Next Iteration Loop The previous slide’s code will

A nested For … To … Next Iteration Loop The previous slide’s code will produce: n Outer. Number variable is 1 Inner. Number variable is 2 n Outer. Number variable is 2 Inner. Number variable is 1 Inner. Number variable is 2 n Outer. Number variable is 3 Inner. Number variable is 1 Inner. Number variable is 2 n Outer. Number variable is 4 Inner. Number variable is 1 Inner. Number variable is 2 1/18/2022 7

Constants Used when: n n The user does not provide the value. You don’t

Constants Used when: n n The user does not provide the value. You don’t want the value to change during the program. e. g. Const Tax. Rate = 0. 25 If you try to assign another value to a constant later in the program an error will occur. 1/18/2022 8

Constants Using a constant rather than a number presents two advantages: n n 1/18/2022

Constants Using a constant rather than a number presents two advantages: n n 1/18/2022 It allows the value to be changed by the programmer easily i. e. otherwise the programmer would have to go through and change the value every time it occurs. It makes the code more meaningful to anybody reading it. 9

To output to the same line / Create a new empty line / Display

To output to the same line / Create a new empty line / Display Multiple Spaces: Console. Write(“…. ”) n Will output onto the same previous line so basically continues a line of outputs. If no previous line then it will output onto a line but without creating a new one. Console. Write. Line() n Will start a new empty line. To enter spaces: n Space(number)

Program 3. 2 a Addition Table Specification: n 1/18/2022 Write a program to display

Program 3. 2 a Addition Table Specification: n 1/18/2022 Write a program to display the sum of row and column numbers. 11

Program 3. 2 a Addition Table Create a new project named ‘Addition Table’. 1/18/2022

Program 3. 2 a Addition Table Create a new project named ‘Addition Table’. 1/18/2022 12

Program 3. 2 a Addition Table Const Max = 5 Dim Col. Number As

Program 3. 2 a Addition Table Const Max = 5 Dim Col. Number As Integer Dim Row. Number As Integer Dim Sum As Integer 'Display '+' and '12' spaces in top right corner to start table. Console. Write("+" & Space(12) ) For Col. Number = 0 To Max 'Simple For. . . To. . . Next loop n n 'Enter column numbers with 8 spaces in between. Console. Write(Col. Number & Space(8)) Next Col. Number 1/18/2022 Continued on next slide. 13

Program 3. 2 a Addition Table Console. Write. Line() ’Start a new empty line.

Program 3. 2 a Addition Table Console. Write. Line() ’Start a new empty line. For Row. Number = 0 To Max 'Start outer loop. n n n 'Enter first number in the row. Console. Write(Row. Number & Space(12)) For Col. Number = 0 To Max 'Start of inner loop. Sum = Col. Number + Row. Number 'Enter addition Console. Write(Sum & Space(8)) n n Next Col. Number 'End of inner loop. Console. Write. Line() ’Start a new empty line. Next Row. Number 'End of outer loop. 1/18/2022 14

Program 3. 2 a Multiplication Table Run the program and test it. 1/18/2022 15

Program 3. 2 a Multiplication Table Run the program and test it. 1/18/2022 15

Given pseudocode will use the following structure: FOR <identifier> ← <value 1> TO <value

Given pseudocode will use the following structure: FOR <identifier> ← <value 1> TO <value 2> STEP <value 3> n <statement(s)> ENDFOR

Extension “Multiplication” Program 3. 2 b Write a program to produce the following multiplication

Extension “Multiplication” Program 3. 2 b Write a program to produce the following multiplication table. Note you will have a problem with columns due to numbers >99. n 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. 1/18/2022 17

The Step Parameter Used to change the variable identifier in a For … To

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) n (Loop Body statements) … Next (variable identifier) 1/18/2022 18

Extension “Between Two Numbers” Program 3. 2 c Extend the “Between Two Numbers” Program

Extension “Between Two Numbers” Program 3. 2 c Extend the “Between Two Numbers” Program from 3. 1 For To Next Loops so that it has a 2 options: 1. As before: to count up from a lower first number to a higher second number. 2. Count down from a higher first number to a lower second number. Hints: n n n Ask the user for option 1 or 2, before the main code. New If statement, just after this, to see which option has been chosen. Leave the original main code for option 1. Copy and paste the original main code into the Else. If for option 2 and adjust as necessary. To count down write Step -1 at the end of the For … To … Step -1 Extension: n n 1/18/2022 Show only numbers between the two values not the values themselves. 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 VB skipping the loop. The new “second” option should not allow the second number to be higher than the first number. 19

Plenary What is the step parameter used for? n 1/18/2022 Used to increase the

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

Plenary O u t e r L o o p What is the general

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