Download presentation
Presentation is loading. Please wait.
Published byEeva-Kaarina Aho Modified over 6 years ago
1
3.1 Iteration Loops For … To … Next 18/01/2019
2
Learning Objectives Define a program loop. State when a loop will end.
State when the For … To … Next iteration loop should be used. State the general form of the For … To … Next loop. 18/01/2019
3
Iteration / Loops Sections of code that may be repeatedly executed.
Contains a boolean condition that determines when it terminates. 18/01/2019
4
Types of Iteration Loops
For … To … Next Is a “count controlled” loop. Do While (condition is true) … Loop Is a “post-condition” loop. Do …. Loop Until (condition is true) Is a “pre-condition” loop. Loop Body The code inside the loop (inserted in place of the dots between ). 18/01/2019
5
For … To … Next Used when you know exactly how many times the code must be repeated. Is a “count controlled” loop. e.g. Display the numbers from 1 to 10. Dim Number As Integer For Number = 1 To 10 Console.Writeline(Number) Next Number 18/01/2019
6
For … To … Next The first time: is executed, Number is set to 1.
For Number = 1 To 10 is executed, Number is set to 1. The loop body code is then executed and then number one is displayed. The line: Next Number indicates the end of the loop and the variable number is incremented by 1 to 2 and program loops back to the first line: The loop body code is then executed again and this time the number two is displayed. This process continues until the loop has been executed exactly 10 times. 18/01/2019
7
For … To … Next General Form
For (variable identifier = start value) To (end value) (Loop Body statements) … Next (variable identifier) Note: Start and End values may be integer constants, variables or expressions. The variable identifier in the last line of the loop is optional but it is good practice to include it as it may your code easier to read. 18/01/2019
8
Program 3.1a Multiplication Table
Specification: Ask the user to enter a number and then output the multiplication table for their number. 18/01/2019
9
Program 3.1a Multiplication Table
Create a new project named ‘Multiplication Table’. 18/01/2019
10
Program 3.1a Multiplication Table
Dim Number As Integer Dim Index As Integer Dim Result As Integer Console.WriteLine(“Enter a number from 1 to 12.”) Number = Console.ReadLine For Index = 1 To 12 ‘Repeat the following from 1 to 12. Result = Index * Number Console.WriteLine(Index & " x " & Number & " = " & Result) Next Index
11
Program 3.1a Multiplication Table
Run the program and test it. 18/01/2019
12
Given pseudocode will use the following structure:
FOR <identifier> ← <value1> TO <value2> <statement(s)> ENDFOR alternatively: FOR <identifi er> ← <value1> TO <value2> STEP <value3>
13
Recalculating a Variable's Value from its previous Value
To recalculate with a constant value: e.g. Number += 1 Each time this line is executed it add 1 to the variable Number each time. If Number starts at 0, Number will increment from: 0 – 1 – 2 – 3 – …. You can also can use: Number -= … Number *= … Number /= … 18/01/2019
14
Recalculating a Variable's Value from its previous Value
To recalculate with another variable simply replace the constant value with the variable: e.g. Result += NewNumber You can also can use: Result -= NewNumber Result *= NewNumber Result /= NewNumber 18/01/2019
15
Commenting on Iteration Loops
For presentations 3.1 – 3.4 I will only ask for comments to loops. Your comments MUST explain: What are you looping for? What are conditions for the loop? e.g. How many times does it loop? Why does it start there and end there? When (after and before what) are you looping and why does it have to be there? 18/01/2019
16
Given pseudocode will use the following structure:
FOR <identifier> ← <value1> TO <value2> <statement(s)> ENDFOR
17
Extension 3.1a The user is expected to type the numbers from 2 to 12. At the moment the user is allowed to enter any number. Stop the user entering any other numbers other than those from 2 to 12 inclusive. 18/01/2019
18
Extension “Between Two Numbers” Program 3.1b
Write a program that: Asks the user for two different numbers. Shows all the numbers from the first value to the second value in a list box. 6 6 7 8 9 The numbers shown here are examples only. 9 18/01/2019 Extended on the next slide.
19
Extension “Between Two Numbers” Program 3.1b
Show only numbers between the two values not the values themselves. What happens if the second number is lower than the first number? The loop would not end this is called an infinite loop, Visual Basic will “see” this and just not execute the loop at all. However, you are expected to avoid them in the first place. Stop the user entering a second number which is lower than the first number they entered. 18/01/2019
20
Extension “Factorial” Program 3.1c
Write a program to work out the factorial of a number entered by the user. e.g. If 4 is entered then the program should calculate 1*2*3*4 = 24 This is the number of combinations e.g. for 4 letters abcd there are 24 ways of arranging these letters. Hints: ‘Declare the variable Result as an integer but give it an initial value of 1 (instead of 0 as is normally the case). Dim Result As Integer = 1 Start your loop from 2 to the number entered as Result is already 1 to begin with so: 1*2*…. to the number entered. ‘Carry on the Result from last time e.g. 1*2*3*4 …. by using the following line: Result *= Extension: What happens if a negative number is entered? Stop a negative number being entered. Make sure you allow for the factorial of 1 though but avoid an infinite loop.
21
Extension “Adding Numbers from 1 to a chosen number” Program 3.1d
Write a program to “add all numbers from 1 to a chosen number”. e.g. If 4 is entered then = 10 If 6 is entered then = 21 etc..... Extension: What happens if a zero or negative number is entered? Stop a zero or a negative number being entered. 18/01/2019
22
Extension “Adding Numbers from one chosen number to another chosen number” Program 3.1e
Write a program to “add all numbers between two chosen numbers”. e.g. If 4 and 7 are entered then = 22 If 10 and 12 are entered then = 33 etc..... Extension: What happens if a negative number is entered? Should you stop a zero or a negative number being entered? What should you stop? 18/01/2019
23
Extension “Adding Numbers from one chosen number to 6 then add 1” Program 3.1f
Write a program to “one chosen number to 6 then add 1”. e.g. If 4 is entered then = 16 If 5 is entered then = 12 Make sure 7 can be entered as the result should = 1 etc..... Extension: What happens if 8 or more is entered? Do not allow the user to do this. 18/01/2019
24
Plenary What is a program loop? How does the loop terminate?
Sections of code that may be repeatedly executed. How does the loop terminate? Contains a boolean condition that determines when it terminates. When should the For … To … Next loop be used? Used when you know exactly how many times the code must be repeated. 18/01/2019
25
Plenary What is the general form of the For … To … Next loop?
For (variable identifier = start value) To (end value) (Loop Body statements) … Next (variable identifier) 18/01/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.