Presentation is loading. Please wait.

Presentation is loading. Please wait.

08/10/20151 5.1 Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.

Similar presentations


Presentation on theme: "08/10/20151 5.1 Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For."— Presentation transcript:

1 08/10/20151 5.1 Iteration Loops For … To … Next

2 208/10/2015 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. Describe validation and explain how to do it. State the general form of code which will check if the contents of a control are numeric. Also state what is returned if numeric and what is returned if not. Also state what is returned if numeric and what is returned if not.

3 308/10/2015 Iteration / Loops Sections of code that may be repeatedly executed. Contains a boolean condition that determines when it terminates.

4 408/10/2015 Types of Iteration Loops For … To … Next Do While (condition is true) … Loop Do …. Loop Until (condition is true) 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 508/10/2015 For … To … Next Used when you know exactly how many times the code must be repeated. 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 10 lblNumber.Text = Number Next Number Next Number

6 608/10/2015 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 in the text label lblNumber. 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 in the text label lblNumber. This process continues until the loop has been executed exactly 10 times.

7 708/10/2015 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 808/10/2015Validation Checking what the user enters obeys predefined rules. e.g. enters numbers between … and …. and is numeric. e.g. enters numbers between … and …. and is numeric. The contents of the text box must be checked directly using an If … End If structures before storing it in a variable. The contents of the text box must be checked directly using an If … End If structures before storing it in a variable. Otherwise you may get errors. E.g. If the program is asked to store letters in a numeric variable. E.g. If the program is asked to store letters in a numeric variable.

9 908/10/2015 Checking if the contents of a control is numeric If IsNumeric(txtName.Text) = False Then If it is false then the text box contains letters or other symbols which make its contents not numeric. If it is true then the text box contains only numbers. Name of the text box

10 1008/10/2015 Program 5.1 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.

11 1108/10/2015 Program 5.1 Multiplication Table Create a new project named ‘Multiplication Table’. Change the form’s Text property to ‘Multiplication Table’.

12 1208/10/2015 Multiplication Table Two labels. One text box. One list box. One button. Set their text / item properties as shown on the form opposite. Note: Drag the form size handles to make the form longer in order to see the full times table without having to scroll down. Drag the form size handles to make the form longer in order to see the full times table without having to scroll down.

13 1308/10/2015 Program 5.1 Multiplication Table Name the text box txtNumber, the list box lstTable and the button butOK.

14 14 Program 5.1 Multiplication Table butOK code: Dim Number As Integer Dim Number As Integer Dim Index As Integer Dim Index As Integer Dim Result As Integer Dim Result As Integer ‘Has the user entered a number? If not warn the user, clear the text ‘Has the user entered a number? If not warn the user, clear the text ‘box and put the cursor into txtNumber. As the End If has put at the ‘box and put the cursor into txtNumber. As the End If has put at the ‘end if the user has not entered a number then the procedure will just ‘end if the user has not entered a number then the procedure will just ‘end. Otherwise continue. ‘end. Otherwise continue. If IsNumeric(txtNumber.Text) = False Then If IsNumeric(txtNumber.Text) = False Then MsgBox(“You must enter a number!”) txtNumber.Clear() txtNumber.Focus() Else ElselstTable.Items.Clear() Number = txtNumber.Text For Index = 1 To 12 ‘Repeat the following from 1 to 12. Result = Index * Number Result = Index * Number lstTable.Items.Add(Index & " x " & Number & " = " & Result) lstTable.Items.Add(Index & " x " & Number & " = " & Result) Next Index End If End If

15 1508/10/2015 Program 5.1 Multiplication Table Run the program and test it.

16 1608/10/2015 Commenting on Iteration Loops From presentations 5.1 – 5.4 I will only ask for comments to loops. 5.15.45.15.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? When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?

17 1708/10/2015 Extension 1 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.

18 1808/10/2015 Extension “Between Two Numbers” Program 2 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. Enter First Number: Second Number: 6789 9 6 The numbers shown here are examples only.

19 1908/10/2015 Extension “Between Two Numbers” Program 2 Extension: Show only numbers between the two values not the values themselves. Show only numbers between the two values not the values themselves. Stop the user entering letters. Stop the user entering letters. 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. 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.

20 2008/10/2015 Extension “Factorial” Program 3 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.

21 2108/10/2015 Extension “Adding Numbers from 1 to a chosen number” Program 4 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.

22 2208/10/2015 Extension “ Adding Numbers from one chosen number to another chosen number ” Program 5 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?

23 2308/10/2015 Extension “ Adding Numbers from one chosen number to 6 then add 1 ” Program 6 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.

24 2408/10/2015 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.

25 2508/10/2015 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)

26 2608/10/2015 Plenary What is validation, how is it done and how do we stop errors like the program attempting to store letters in a number variable?

27 2708/10/2015Validation Checking what the user enters obeys predefined rules. e.g. enters numbers between … and …. and is numeric. e.g. enters numbers between … and …. and is numeric. The contents of the text box must be checked directly using an If … End If structures before storing it in a variable. The contents of the text box must be checked directly using an If … End If structures before storing it in a variable. Otherwise you may get errors. E.g. If the program is asked to store letters in a numeric variable. E.g. If the program is asked to store letters in a numeric variable.

28 2808/10/2015 Plenary What is the general form of code which will check if the contents of a control are numeric? What is returned if numeric and what is returned if not? What is returned if numeric and what is returned if not?

29 2908/10/2015 Checking if the contents of a control is numeric IsNumeric(….Text) Returns True if numeric and False if it is not. Name of control


Download ppt "08/10/20151 5.1 Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For."

Similar presentations


Ads by Google