Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Repeating Actions While and For Loops
Computer Science 1620 Loops.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
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: Loops and Files.
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
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.
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.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
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 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.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Visual Basic Programming
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
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.
6.2 For…Next Loops General Form of a For…Next Loop
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ 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 -
Repetition Structures
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
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.
Chapter six exercises
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Controlling Program Flow with Looping Structures
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 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
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.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
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.
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 2.2 Control Structures (Iteration)
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter (3) - Looping Questions.
Chapter 6 - VB.Net by Schneider
Topics Introduction to Repetition Structures
For...Next Statements.
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

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 multiple times until a criteria is met

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

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 Example (Displays the numbers from 1 through 10) Private Sub cmdDisplay_Click() Dim num As Integer ' Display the numbers from 1 to 10 num = 1 Do While num <= 10 picNumbers.Print num; num = num + 1 Loop End Sub

Chapter 6 - Visual Basic Schneider9 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.

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

Chapter 6 - Visual Basic Schneider12 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 incremented (or decremented) by the same value during each repetition.

Chapter 6 - Visual Basic Schneider13 Example num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop

Chapter 6 - Visual Basic Schneider14 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 Schneider15 Example (requires the user to give a password before opening a file) Private Sub cmdDisplay_Click() Dim passWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then Do passWord = UCase(InputBox("What is the password?")) Loop Until passWord = "SHAZAM" End If Open txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1 End Sub

Chapter 6 - Visual Basic Schneider16 Example (years to deplete a saving account) Private Sub cmdEstimate_Click() Dim amt As Single, yrs As Integer ' Years to deplete savings account picResult.Cls amt = yrs = 0 Do amt = amt * yrs = yrs + 1 Loop Until amt <= 0 picResult.Print "It takes"; yrs; "years to deplete the account." End Sub

Chapter 6 - Visual Basic Schneider17 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 Schneider18 EOF Function u EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False u Example: 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 Schneider19 Counters and Accumulators u A counter is a numeric variable that keeps track of the number of items that have been processed in a loop. u An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop.

Chapter 6 - Visual Basic Schneider20 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." End Sub

Compare u Do While ……. Loop u Do ……. Loop While u Do ……. Loop Until u Do Until ……. Loop Chapter 6 - Visual Basic Schneider21

Review num = 11 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Chapter 6 - Visual Basic Schneider22 num = 11 Do picOutput.Print num; num = num + 1 Loop until num <= 10 How many times will the following loops execute?

Review Do While i < 10 i = i + 1 Loop Chapter 6 - Visual Basic Schneider23 Do i = i + 1 Loop While i < 10 Which loop is infinite? Do Until i < 10 Loop Do i = i + 10 Loop Until i < 10

Chapter 6 - Visual Basic Schneider24 For … Next Loop u A loop where the number of iterations is determined by a range of values for a numeric variable u Syntax: For controlVariable = initial To terminal statement(s) Next controlVariable

Chapter 6 - Visual Basic Schneider25 Example Private Sub cmdDisplayTable_Click() Dim i As Integer ‘Display a table of the first 5 numbers and their squares For i = 1 To 5 picTable.Print i; i ^ 2 Next i End Sub Initial Value Control variable Terminating value

Chapter 6 - Visual Basic Schneider26 Example Dim numVar As Integer For numVar = 1 To 5 Step 2 picOutput.Print numVar; Next numVar Output: 1 3 5

Chapter 6 - Visual Basic Schneider27 When a For statement is encountered 1. The control variable is assigned the initial value. 2. After each loop iteration, the step value is added to the value of the control variable. (If there is no step value, 1 is added.) 3. Iteration continues until the terminating value is exceeded.

Chapter 6 - Visual Basic Schneider28 Rules for Using For... Next loop u You should never modify the value of the loop control variable in the loop body. u Each For loop must end with a Next statement.

Chapter 6 - Visual Basic Schneider29 Example Private Sub cmdDisplay_Click() Dim i As Integer For i = 1 To 10 picOutput.Print "*"; Next i End Sub Output: **********

Chapter 6 - Visual Basic Schneider30 Example Private Sub cmdDisplay_Click() Dim i As Integer, stars As Integer stars = Val(InputBox("Row length (1-20) : ")) For i = 1 To stars picOutput.Print "*"; Next i End Sub

Chapter 6 - Visual Basic Schneider31 Example Dim numVar As Integer For numVar = 8 To 1 Step -2 picOutput.Print numVar; Next numVar Output:

Chapter 6 - Visual Basic Schneider32 Nested Loops For outer = 1 To 4 For inner = 1 To 2.. Next inner Next outer

Chapter 6 - Visual Basic Schneider33 Example: Display a 10x10 rectangle of stars Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub

Review For i= -5 To -1 Step - 2 picOutput.Print i; Next i picOutput.Print i; Chapter 6 - Visual Basic Schneider34 For i= 1 To 5 Step 2 i=i+1 picOutput.Print i; Next i picOutput.Print i;

Chapter 6 - Visual Basic Schneider35 Review Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = i To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To i picOutput.Print "*"; Next j picOutput.Print Next i End Sub