Presentation is loading. Please wait.

Presentation is loading. Please wait.

26/06/20161 3.1 Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.

Similar presentations


Presentation on theme: "26/06/20161 3.1 Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For."— Presentation transcript:

1 26/06/20161 3.1 Iteration Loops For … To … Next

2 226/06/2016 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.

3 326/06/2016 Iteration / Loops Sections of code that may be repeatedly executed. Contains a boolean condition that determines when it terminates.

4 426/06/2016 Types of Iteration Loops For … To … Next Is a “count controlled” loop. Is a “count controlled” loop. Do While (condition is true) … Loop Is a “post-condition” loop. Is a “post-condition” loop. Do …. Loop Until (condition is true) Is a “pre-condition” loop. Is a “pre-condition” loop. Loop Body The code inside the loop (inserted in place of the dots between ). The code inside the loop (inserted in place of the dots between ).

5 526/06/2016 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 Dim Number As Integer For Number = 1 To 10 For Number = 1 To 10Console.Writeline(Number) Next Number Next Number

6 626/06/2016 For … To … Next The first time: For Number = 1 To 10 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 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: For Number = 1 To 10 For Number = 1 To 10 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.

7 726/06/2016 For … To … Next General Form For (variable identifier = start value) To (end value) (Loop Body statements) … (Loop Body statements) … Next (variable identifier) Note: Start and End values may be integer constants, variables or expressions. 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. 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.

8 826/06/2016 Program 3.1a Multiplication Table Specification: Ask the user to enter a number and then output the multiplication table for their number. Ask the user to enter a number and then output the multiplication table for their number.

9 926/06/2016 Program 3.1a Multiplication Table Create a new project named ‘Multiplication Table’.

10 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 Result = Index * Number Console.WriteLine(Index & " x " & Number & " = " & Result) Console.WriteLine(Index & " x " & Number & " = " & Result) Next Index

11 1126/06/2016 Program 3.1a Multiplication Table Run the program and test it.

12 Given pseudocode will use the following structure: FOR ← TO ENDFOR alternatively: FOR ← TO STEP ENDFOR

13 1326/06/2016 Commenting on Iteration Loops For presentations 3.1 – 3.4 I will only ask for comments to loops. 3.1 – 3.43.1 – 3.4 Your comments MUST explain: What are you looping for? What are you looping for? What are conditions for the loop? What are conditions for the loop?e.g. How many times does it loop? How many times does it loop? Why does it start there and end there? Why does it start there and end there? When (after and before what) are you looping and why does it have to be there? When (after and before what) are you looping and why does it have to be there?

14 Given pseudocode will use the following structure: FOR ← TO ENDFOR

15 1526/06/2016 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. Stop the user entering any other numbers other than those from 2 to 12 inclusive.

16 1626/06/2016 Extension “Between Two Numbers” Program 3.1b Write a program that: Asks the user for two different numbers. Asks the user for two different numbers. Shows all the numbers from the first value to the second value in a list box. Shows all the numbers from the first value to the second value in a list box. Extended on the next slide. 6789 9 6 The numbers shown here are examples only.

17 1726/06/2016 Extension “Between Two Numbers” Program 3.1b Extension: Show only numbers between the two values not the values themselves. Show only numbers between the two values not the values themselves. What happens if the second number is lower than the first number? 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. 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. Stop the user entering a second number which is lower than the first number they entered.

18 18 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 e.g. If 4 is entered then the program should calculate 1*2*3*4 = 24 This is the number of combinations 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). ‘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. 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: ‘Carry on the Result from last time e.g. 1*2*3*4 …. by using the following line: Result = Result * Extension: What happens if a negative number is entered? 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. Make sure you allow for the factorial of 1 though but avoid an infinite loop.

19 1926/06/2016 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. e.g. If 4 is entered then 1+2+3+4 = 10 If 6 is entered then 1+2+3+4+5+6 = 21 etc.....Extension: What happens if a zero or negative number is entered? What happens if a zero or negative number is entered? Stop a zero or a negative number being entered.

20 2026/06/2016 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. e.g. If 4 and 7 are entered then 4+5+6+7 = 22 If 10 and 12 are entered then 10+11+12 = 33 etc.....Extension: What happens if a negative number is entered? What happens if a negative number is entered? Should you stop a zero or a negative number being entered? What should you stop? What should you stop?

21 2126/06/2016 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. e.g. If 4 is entered then 4+5+6+1 = 16 If 5 is entered then 5+6+1 = 12 Make sure 7 can be entered as the result should = 1 etc.....Extension: What happens if 8 or more is entered? What happens if 8 or more is entered? Do not allow the user to do this.

22 2226/06/2016 Plenary What is a program loop? Sections of code that may be repeatedly executed. 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. Used when you know exactly how many times the code must be repeated.

23 2326/06/2016 Plenary What is the general form of the For … To … Next loop? For (variable identifier = start value) To (end value) For (variable identifier = start value) To (end value) (Loop Body statements) … (Loop Body statements) … Next (variable identifier) Next (variable identifier)


Download ppt "26/06/20161 3.1 Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For."

Similar presentations


Ads by Google