Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
MsgBox Function Displays one of Visual Basic’s predefined dialog boxes, which contains a message, one or more command buttons, and an icon After displaying.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
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.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Programming Logic and Design Sixth Edition Chapter 5 Looping.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Chapter 6: The Repetition Structure
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
1.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter (3) - Looping Questions.
CIS 16 Application Development Programming with Visual Basic
Chapter 6 - VB.Net by Schneider
Topics Introduction to Repetition Structures
Presentation transcript:

Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition

Chapter 6 - Visual Basic Schneider2 Outline & Objectives u Loop Structure u Elements of a Loop Structure u Processing Lists of Data with Do Loops u Option buttons vs. Check boxes u Multiple forms

Chapter 6 - Visual Basic Schneider3 Types of LOOP Structures u Do While ……. Loop u Do Until …… Loop u For …… Next loop

Chapter 6 - Visual Basic Schneider4 Basic Definition u Looping : the process of repeating a series of statements as many times as needed. u Looping also called iteration.

Chapter 6 - Visual Basic Schneider5 Basic Components of Loops u Loop control variable: A variable used to determine whether a loop will be executed u Loop body: The statement (s) that are executed each time a loop repeats

Chapter 6 - Visual Basic Schneider6 The Do While ……. Loop Do While condition is true statement(s) Loop u Compare this to IF …. Then

Chapter 6 - Visual Basic Schneider7 Flowchart for a Do While Loop Is the condition true Execute statements within the loop Execute statements that follow the loop Yes No

Chapter 6 - Visual Basic Schneider8 The Do While ……. Loop u Is executed as long as the condition is True. u If condition is False then the next statement after the Loop is executed. u Step Demo DoCount

Chapter 6 - Visual Basic Schneider9 Controlling Loops u Methods of controlling loops: u Counter-controlled loops u repeat a specific number of times u Event-controlled loops u repeat until something happens in the loop body to change the value of loop control variable.

Chapter 6 - Visual Basic Schneider10 Example of event-controlled loops passWord = "" Do While passWord <> "SHAZAM" passWord = UCase(InputBox("What is the password?")) Loop

Chapter 6 - Visual Basic Schneider11 Counter-controlled Loops u Is useful when the programmer knows how many times the loop should be executed. u Initialize the counter by setting it to a beginning value before entering the loop. u The counter is increased (or decreased) by the same value during each repetition.

Chapter 6 - Visual Basic Schneider12 Example num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Watch out for infinite loops!

Chapter 6 - Visual Basic Schneider13 Do Until ……. Loop u Is executed until the condition becomes True u Any Do While…. Loop can be rewritten as a Do Until ….. Loop

Chapter 6 - Visual Basic Schneider14 Example (requires the user to give a password before opening a file) Do passWord = UCase(InputBox("What is the password?")) Loop Until passWord = "SHAZAM“ Demo - CTRL+Break

Chapter 6 - Visual Basic Schneider15 Example (years to deplete a saving account) Jane has saved $150,000 for retirement. She is now 65 and wants to draw $15,000 per year to supplement her pension. She is earning interest on her account at the rate of 7.5% per year. How old will Jane be before all her savings are used up? Demo Savings

Chapter 6 - Visual Basic Schneider16 Comparing While… and Until Loops u The Do While … Loop executes while the condition is true u The Do Until….. Loop executes until the condition is true u Both can be used to create any type of loop

Chapter 6 - Visual Basic Schneider17 Schneider p

Chapter 6 - Visual Basic Schneider18 Processing List of Data with Loops u EOF Function u The EOF function tells us if we have read to the end of a file. u Checks for the end-of-file marker.

Chapter 6 - Visual Basic Schneider19 EOF Function u EOF(n) where n is the reference number for the file. u If there are more records to be read, EOF is False. u When the end-of-file marker is reached; EOF becomes True.

Chapter 6 - Visual Basic Schneider20 Example of EOF Open “Phone.txt” for Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNum Loop Close #1

Chapter 6 - Visual Basic Schneider21 Counters and Accumulators u A Counter is a numeric variable that keeps track of the number of items that have been processed in the loop. u An Accumulator is a numeric variable that totals numbers. u Demo Average

Chapter 6 - Visual Basic Schneider22 Example: Counter & Accumulator Private Sub cmdAnalyze_Click() Dim numCoins As Integer, sum As Single, value As Single Open "COINS.TXT" For Input As #1 numCoins = 0 sum = 0 Do While Not EOF(1) Input #1, value numCoins = numCoins + 1 sum = sum + value Loop picValue.Print "The value of the"; numCoins; "coins is"; sum; "cents." Close #1 End Sub

Chapter 6 - Visual Basic Schneider23 Example of a Data File 1, 1, 5, 10, 10, 25

Chapter 6 - Visual Basic Schneider24 Output for the Previous Example The value of the 6 coins is 52 cents.

Chapter 6 - Visual Basic Schneider25 For ……... Next Loop u Is used to create a counting loop. u Loop control variable has an initial value. u Loop control variable has a terminating value. u Loop control variable has a Step value.

Chapter 6 - Visual Basic Schneider26 Example ( display a table of the first 5 numbers and their square) For i = 1 To 5 picTable.Print i; i ^ 2 Next i Demo counter Initial Value Loop Control variable Terminating value

Chapter 6 - Visual Basic Schneider27 Rules for Using For... Next loop u The initial, terminal, and step values cannot be modified in the loop body. u You should never modify the value of the loop control variable in the loop body. u Each For statement must end with a Next statement.

Chapter 6 - Visual Basic Schneider28 Example (the step value is negative) For Counter 8 To 1 Step -2 picOutput.Print Counter Next Counter

Chapter 6 - Visual Basic Schneider29 Nested Loops For Outer = 1 To 4 For Inner = 1 To 2.. Next Inner Next Outer

Chapter 6 - Visual Basic Schneider30 For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next I What wil this code do?

Chapter 6 - Visual Basic Schneider31 Schneider p. 268 – 275 Schneider p

Chapter 6 - Visual Basic Schneider32 Option Buttons u Used in situations where you want to limit the user to only one of two or more related and mutually exclusive choices u Only one in a group can be selected (on) at any one time u When selected, an option button’s Value property contains the Boolean value True; otherwise it contains the Boolean value False

Chapter 6 - Visual Basic Schneider33 Option Buttons u Minimum number in an interface is two u Recommended maximum number is seven u A default button should be selected when the interface first appears (use form_load event) u Demo State_opt

Chapter 6 - Visual Basic Schneider34 Frame Control u Acts as a container for other controls u Used to visually separate controls from one another u The frame and the controls contained within the frame are treated as one unit u You must use a frame control if you want to have more than one group of option buttons (Demo)

Chapter 6 - Visual Basic Schneider35 Check Box u Used in situations where you want to allow the user to select any number of choices from one or more independent and non-exclusive choices u Any number of check boxes can be selected at any one time u When selected, a check box’s Value property contains the number 1 (vbChecked). When unselected, it contains the number 0 (vbUnchecked)

Chapter 6 - Visual Basic Schneider36 Check Box u Assign a unique access to each check box u You also can use the spacebar to select/deselect a check box that has the focus u Unlike option buttons, you must test a checkbox when it is clicked to see if it is checked or unchecked

Chapter 6 - Visual Basic Schneider37 u Demo Rentals_opt u Demo Math and Math2

Chapter 6 - Visual Basic Schneider38 Multiple Forms u Adding a second form u Form load and unload commands u Form hide and show commands u Syntax - referring to controls on other forms u Demo - Order