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.

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
5.05 Apply Looping Structures
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
08/09/ Arrays Defining, Declaring & Processing.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Chapter 12: How Long Can This Go On?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
Tutorial 6 The Repetition Structure
30/10/ Iteration Loops Do While (condition is true) … Loop.
Introduction to Programming with RAPTOR
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
22/11/ Selection If selection construct.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items.
31/01/ Selection If selection construct.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Controlling Program Flow with Looping Structures
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Unit 6 Repetition Processing Instructor: Brent Presley.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Controlling Program Flow with Decision Structures.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
21/03/ Working with Controls Text and List Boxes.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Conditions and Ifs BIS1523 – Lecture 8.
Chapter (3) - Looping Questions.
If selection construct
Do While (condition is true) … Loop
Coding Concepts (Basics)
4.1 Strings ASCII & Processing Strings with the Functions
If selection construct
Do … Loop Until (condition is true)
Text / Serial / Sequential Files
Fundamentals of visual basic
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
Review of Previous Lesson
10.3 Procedures Function Procedures 07/06/2019.
Text / Serial / Sequential Files
The Step Parameter & Nested For … To … Next loops
3.2 Working with Data Scope of variables 29/07/2019.
Presentation transcript:

08/10/ Iteration Loops For … To … Next

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.

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

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 ).

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

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.

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.

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.

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

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.

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

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.

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

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

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

1608/10/2015 Commenting on Iteration Loops From presentations 5.1 – 5.4 I will only ask for comments to loops 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…)?

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.

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: The numbers shown here are examples only.

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.

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.

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 = 10 If 6 is entered then = 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.

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 = 22 If 10 and 12 are entered then = 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?

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 = 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? What happens if 8 or more is entered? Do not allow the user to do this.

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.

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)

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?

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.

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?

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