08/10/20151 5.1 Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a...

29
23/06/22 23/06/22 1 5.1 Iteration 5.1 Iteration Loops Loops For … To … Next For … To … Next

Transcript of 08/10/20151 5.1 Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a...

Page 1: 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.

21/04/2321/04/23 11

5.1 Iteration Loops5.1 Iteration Loops

For … To … NextFor … To … Next

Page 2: 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.

2221/04/2321/04/23

Learning ObjectivesLearning Objectives

Define a program loop.Define a program loop.State when a loop will end.State when a loop will end.State when the State when the For … To … NextFor … To … Next iteration loop iteration loop should be used.should be used.State the general form of the State the general form of the For … To … NextFor … To … Next loop.loop.Describe validation and explain how to do it.Describe validation and explain how to do it.State the general form of code which will check if State the general form of code which will check if the contents of a control are numeric.the contents of a control are numeric. Also state what is returned if numeric and what is Also state what is returned if numeric and what is

returned if not.returned if not.

Page 3: 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.

3321/04/2321/04/23

Iteration / LoopsIteration / Loops

Sections of code that may be repeatedly Sections of code that may be repeatedly executed.executed.

Contains a boolean condition that Contains a boolean condition that determines when it terminates.determines when it terminates.

Page 4: 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.

4421/04/2321/04/23

Types of Iteration LoopsTypes of Iteration Loops

For … To … NextFor … To … Next

Do WhileDo While (condition is true)(condition is true) … … LoopLoop

Do …. Loop UntilDo …. Loop Until (condition is true)(condition is true)

Loop BodyLoop Body The code inside the loop (inserted in place of The code inside the loop (inserted in place of

the dots between ).the dots between ).

Page 5: 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.

5521/04/2321/04/23

For … To … NextFor … To … Next

Used when you know exactly how many Used when you know exactly how many times the code must be repeated.times the code must be repeated.

e.g. Display the numbers from 1 to 10.e.g. Display the numbers from 1 to 10. Dim Number As IntegerDim Number As Integer For Number = 1 To 10For Number = 1 To 10

lblNumber.Text = NumberlblNumber.Text = Number Next NumberNext Number

Page 6: 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.

6621/04/2321/04/23

For … To … NextFor … To … NextThe first time: The first time:

For Number = 1 To 10For Number = 1 To 10

is executed, Number is set to 1.is executed, Number is set to 1.The loop body code is then executed and then number one is The loop body code is then executed and then number one is displayed in the text label lblNumber.displayed in the text label lblNumber.The line:The line:

Next Number Next Number

indicates the end of the loop and the variable number is indicates the end of the loop and the variable number is incremented by 1 to 2 and program loops back to the first line:incremented by 1 to 2 and program loops back to the first line:

For Number = 1 To 10For Number = 1 To 10

The loop body code is then executed again and this time the The loop body code is then executed again and this time the number two is displayed in the text label lblNumber.number two is displayed in the text label lblNumber.This process continues until the loop has been executed This process continues until the loop has been executed exactly 10 times.exactly 10 times.

Page 7: 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.

7721/04/2321/04/23

For … To … NextFor … To … Next General FormGeneral Form

For For (variable identifier = start value)(variable identifier = start value) To To (end value)(end value) (Loop Body statements) …(Loop Body statements) …

Next Next (variable identifier)(variable identifier)

Note:Note: Start and End values may be integer constants, Start and End values may be integer constants,

variables or expressions.variables or expressions. The variable identifier in the last line of the loop is The variable identifier in the last line of the loop is

optional but it is good practice to include it as it may optional but it is good practice to include it as it may your code easier to read.your code easier to read.

Page 8: 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.

8821/04/2321/04/23

ValidationValidation

Checking what the user enters obeys Checking what the user enters obeys predefined rules.predefined rules. e.g. enters numbers between … and …. e.g. enters numbers between … and ….

and is numeric.and is numeric. The contents of the text box must be The contents of the text box must be

checked directly using an If … End If checked directly using an If … End If structures before storing it in a variable.structures before storing it in a variable.

Otherwise you may get errors.Otherwise you may get errors. E.g. If the program is asked to store letters in a E.g. If the program is asked to store letters in a

numeric variable.numeric variable.

Page 9: 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.

9921/04/2321/04/23

Checking if the contents of a Checking if the contents of a control is numericcontrol is numeric

If If IsNumeric(IsNumeric(txtNametxtName.Text).Text) = False Then = False Then

If it is false then the text box contains If it is false then the text box contains letters or other symbols which make its letters or other symbols which make its contents not numeric.contents not numeric.

If it is true then the text box contains only If it is true then the text box contains only numbers.numbers.

Name of the text boxName of the text box

Page 10: 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.

101021/04/2321/04/23

Program 5.1 Multiplication TableProgram 5.1 Multiplication Table

Specification:Specification: Ask the user to enter a number and then Ask the user to enter a number and then

output the multiplication table for their output the multiplication table for their number.number.

Page 11: 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.

111121/04/2321/04/23

Program 5.1 Multiplication TableProgram 5.1 Multiplication Table

Create a new project named Create a new project named ‘‘Multiplication TableMultiplication Table’.’.

Change the form’s Change the form’s TextText property to property to ‘‘Multiplication TableMultiplication Table’.’.

Page 12: 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.

121221/04/2321/04/23

Multiplication TableMultiplication TableTwo labels. Two labels. One text box.One text box.One list box.One list box.One button.One button.Set their text / item Set their text / item properties as shown on properties as shown on the form opposite.the form opposite.Note:Note:

Drag the form size Drag the form size handles to make the form handles to make the form longer in order to see the longer in order to see the full times table without full times table without having to scroll down.having to scroll down.

Page 13: 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.

131321/04/2321/04/23

Program 5.1 Multiplication TableProgram 5.1 Multiplication Table

Name the text box Name the text box txtNumbertxtNumber, the list box , the list box lstTablelstTable and the button and the button butOKbutOK..

Page 14: 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.

1414

Program 5.1 Multiplication TableProgram 5.1 Multiplication TablebutOKbutOK code: code:

Dim Number As IntegerDim Number As Integer Dim Index As IntegerDim Index As Integer Dim Result As IntegerDim 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 If IsNumeric(IsNumeric(txtNumbertxtNumber.Text).Text) = False Then = False Then

MsgBox(“You must enter a number!”)MsgBox(“You must enter a number!”)txtNumbertxtNumber..Clear()Clear()txtNumbertxtNumber..Focus()Focus()

ElseElselstTable.Items.Clear()lstTable.Items.Clear()Number = txtNumber.TextNumber = txtNumber.TextFor Index = 1 To 12 For Index = 1 To 12 ‘Repeat the following from 1 to 12.‘Repeat the following from 1 to 12.

Result = Index * NumberResult = Index * Number lstTable.Items.Add(Index & " x " & Number & " = " & Result)lstTable.Items.Add(Index & " x " & Number & " = " & Result)

Next IndexNext Index End IfEnd If

Page 15: 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.

151521/04/2321/04/23

Program 5.1 Multiplication TableProgram 5.1 Multiplication Table

Run the program and test it.Run the program and test it.

Page 16: 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.

161621/04/2321/04/23

Commenting on Iteration LoopsCommenting on Iteration Loops

From presentations From presentations 5.1 – – 5.4 I will only ask for I will only ask for comments to loops.comments to loops.Your comments MUST explain:Your comments MUST explain: WhatWhat are you looping for? are you looping for? WhatWhat are conditions for the loop? are conditions for the loop?

e.g. e.g. How manyHow many times does it loop? times does it loop? WhyWhy does it start there and end there? does it start there and end there?

WhenWhen ((afterafter andand beforebefore whatwhat)) are you looping and are you looping and whywhy does it have to be there?does it have to be there?

WhenWhen in the procedure in the procedure codecode or, if it is on its own, in which or, if it is on its own, in which procedure (procedure (buttonbutton, , checkboxcheckbox, , textboxtextbox, etc…)?, etc…)?

Page 17: 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.

171721/04/2321/04/23

Extension 1Extension 1

The user is expected to type the numbers The user is expected to type the numbers from 2 to 12. At the moment the user is from 2 to 12. At the moment the user is allowed to enter any number.allowed to enter any number. Stop the user entering any other numbers Stop the user entering any other numbers

other than those from 2 to 12 inclusive.other than those from 2 to 12 inclusive.

Page 18: 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.

181821/04/2321/04/23

Extension “Between Two Numbers” Extension “Between Two Numbers” Program 2Program 2

Write a program that: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 Shows all the numbers from the first value to

the second value in a list box.the second value in a list box.

Extended on the next slide.Extended on the next slide.

EnterEnter

First Number:First Number:

Second Number:Second Number:66

77

88

99

99

66 The numbers shown here The numbers shown here are examples only.are examples only.

Page 19: 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.

191921/04/2321/04/23

Extension “Between Two Numbers” Extension “Between Two Numbers” Program 2Program 2

Extension:Extension: Show only numbers Show only numbers betweenbetween the two values not the the two values not the

values themselves.values themselves. Stop the user entering letters.Stop the user entering letters. What happens if the second number is lower than the What happens if the second number is lower than the

first number? first number? The loop would not end this is called an The loop would not end this is called an infinite loopinfinite loop, Visual , Visual Basic will “see” this and just not execute the loop at all.Basic will “see” this and just not execute the loop at all.

Stop the user entering a second number which is Stop the user entering a second number which is lower than the first number they entered.lower than the first number they entered.

Page 20: 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.

202021/04/2321/04/23

Extension “Factorial” Program 3Extension “Factorial” Program 3Write a program to work out the factorial of a number Write a program to work out the factorial of a number entered by the user.entered by the user.

e.g. If 4 is entered then the program should calculate 1*2*3*4 = e.g. If 4 is entered then the program should calculate 1*2*3*4 = 2424

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.e.g. for 4 letters abcd there are 24 ways of arranging these letters.

Hints:Hints: ‘‘Declare the variable Result as an integer but give it an initial value of 1 Declare the variable Result as an integer but give it an initial value of 1

(instead of 0 as is normally the case).(instead of 0 as is normally the case).Dim Result As Integer = 1 Dim Result As Integer = 1

Start your loop from 2 to the number entered as Result is Start your loop from 2 to the number entered as Result is already 1 to begin with so: 1*2*…. to the number entered.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 Carry on the Result from last time e.g. 1*2*3*4 …. by using the following line:following line:

Result = Result *Result = Result *

Extension:Extension: What happens if a negative number is entered?What happens if a negative number is entered?

Stop a negative number being entered.Stop a negative number being entered.

Page 21: 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.

212121/04/2321/04/23

Extension “Extension “Adding Numbers from Adding Numbers from 1 to a chosen number1 to a chosen number” Program 4” Program 4

Write a program to “add all numbers from 1 to a Write a program to “add all numbers from 1 to a chosen number”.chosen number”. e.g. e.g.

If 4 is entered then 1+2+3+4 = If 4 is entered then 1+2+3+4 = 1010If 6 is entered then 1+2+3+4+5+6 = If 6 is entered then 1+2+3+4+5+6 = 2121etc.....etc.....

Extension:Extension: What happens if a zero or negative number is What happens if a zero or negative number is

entered?entered?Stop a zero or a negative number being entered.Stop a zero or a negative number being entered.

Page 22: 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.

222221/04/2321/04/23

Extension “Extension “Adding Numbers from Adding Numbers from one chosen number to another one chosen number to another

chosen numberchosen number” Program 5” Program 5Write a program to “add all numbers between Write a program to “add all numbers between two chosen numbers”.two chosen numbers”. e.g. e.g.

If 4 and 7 are entered then 4+5+6+7 = 22If 4 and 7 are entered then 4+5+6+7 = 22If 10 and 12 are entered then 10+11+12 = If 10 and 12 are entered then 10+11+12 = 3333etc.....etc.....

Extension: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?Should you stop a zero or a negative number being entered? What should you stop?What should you stop?

Page 23: 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.

232321/04/2321/04/23

Extension “Extension “Adding Numbers from Adding Numbers from one chosen number to 6 then add 1one chosen number to 6 then add 1” ”

Program 6Program 6Write a program to “one chosen number to 6 Write a program to “one chosen number to 6 then add 1”.then add 1”. e.g. e.g.

If 4 is entered then 4+5+6+1 = 16If 4 is entered then 4+5+6+1 = 16If 5 is entered then 5+6+1 = 12If 5 is entered then 5+6+1 = 12Make sure 7 can be entered as the result should = 1Make sure 7 can be entered as the result should = 1etc.....etc.....

Extension: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.Do not allow the user to do this.

Page 24: 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.

242421/04/2321/04/23

PlenaryPlenary

What is a program loop?What is a program loop? Sections of code that may be repeatedly Sections of code that may be repeatedly

executed.executed.

How does the loop terminate?How does the loop terminate?Contains a boolean condition that determines Contains a boolean condition that determines when it terminates.when it terminates.

When should the When should the For … To … NextFor … To … Next loop loop be used?be used? Used when you know exactly how many times Used when you know exactly how many times

the code must be repeated.the code must be repeated.

Page 25: 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.

252521/04/2321/04/23

PlenaryPlenary

What is the general form of the What is the general form of the For … For … To … NextTo … Next loop? loop? For For (variable identifier = start value)(variable identifier = start value) To To (end value)(end value) (Loop Body statements) …(Loop Body statements) …

Next Next (variable identifier)(variable identifier)

Page 26: 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.

262621/04/2321/04/23

PlenaryPlenary

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

Page 27: 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.

272721/04/2321/04/23

ValidationValidation

Checking what the user enters obeys Checking what the user enters obeys predefined rules.predefined rules. e.g. enters numbers between … and …. e.g. enters numbers between … and ….

and is numeric.and is numeric. The contents of the text box must be The contents of the text box must be

checked directly using an If … End If checked directly using an If … End If structures before storing it in a variable.structures before storing it in a variable.

Otherwise you may get errors.Otherwise you may get errors. E.g. If the program is asked to store letters in a E.g. If the program is asked to store letters in a

numeric variable.numeric variable.

Page 28: 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.

282821/04/2321/04/23

PlenaryPlenary

What is the general form of code which will What is the general form of code which will check if the contents of a control are check if the contents of a control are numeric?numeric? What is returned if numeric and what is What is returned if numeric and what is

returned if not?returned if not?

Page 29: 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.

292921/04/2321/04/23

Checking if the contents of a Checking if the contents of a control is numericcontrol is numeric

IsNumeric(IsNumeric(…….Text).Text)

Returns Returns TrueTrue if numeric and if numeric and FalseFalse if it is if it is not.not.

Name of controlName of control