Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.

Slides:



Advertisements
Similar presentations
Objectives In this chapter, you will learn about:
Advertisements

Repeating Actions While and For Loops
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
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.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Chapter 5: Loops and Files.
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.
Objectives You should be able to describe:
 2007 Pearson Education, Inc. All rights reserved C Program Control.
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.
5.05 Apply Looping Structures
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
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.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
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.
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:
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
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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 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.
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 6 Controlling Program Flow with Looping Structures.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 - VB.Net by Schneider
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
For...Next Statements.
Presentation transcript:

Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition

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

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

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

Chapter 6 - Visual Basic Schneider 5 The Do While ……. Loop Do While condition is true statement(s) Loop

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

Chapter 6 - Visual Basic Schneider 8 The Do While ……. Loop Is executed as long as the condition is True. If condition is False then the next statement after the Loop is executed.

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

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

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

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

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

Chapter 6 - Visual Basic Schneider 14 Example (requires the user to give a password before opening a file) Private Sub cmdDisplay_Click() Dim strPassWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then Do strPassWord = UCase(InputBox("What is the password?")) Loop Until strPassWord = "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 Schneider 15 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 Schneider 16 Comparing While… and Until Loops The Do While … Loop executes while the condition is true The Do Until….. Loop executes until the condition is true Both can be used to create any type of loop

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

Chapter 6 - Visual Basic Schneider 18 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 Schneider 19 For ……... Next Loop Is used to create a counting loop. Loop control variable has an initial value. Loop control variable has a terminating value. Loop control variable has a Step value.

Chapter 6 - Visual Basic Schneider 20 Syntax of For…… Next Loop For loop-control-variable = initial To terminal statement(s) Next loop-control-variable

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

Chapter 6 - Visual Basic Schneider 22 Example ( step value of 2) For counter = 1 To 5 Step 2 picOutput.Print counter Next counter

Chapter 6 - Visual Basic Schneider 23 When the For statement is first encountered This explanation assumes a positive step value The initial, terminal, and (if given ) step value expression are evaluated. The loop control variable is assigned to the initial value.

Chapter 6 - Visual Basic Schneider 24 This explanation assumes a positive step value The value of the loop control variable is tested against the terminal value. If the loop control variable is less than or equal to the terminal value, the loop body is executed. If the loop control variable is greater than the terminal value, the loop body is skipped, and control passes to the first statement following the Next.

Chapter 6 - Visual Basic Schneider 25 When the Next statement is encountered The step value is added to the loop control variable. If there is no step value, +1 is added. A check is performed to determine if the value of the loop control variable exceeds the terminal value.

Chapter 6 - Visual Basic Schneider 26 Continued If the loop control variable is less than or equal to the terminal value, control transfers back to the statement after the For statement and the loop is repeated. Otherwise, the loop is exited, and execution continues with the statement following the Next.

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

Chapter 6 - Visual Basic Schneider 28 Example (display 10 stars) Private Sub cmdDisplay_Click() Dim i As Integer ' Display a row of ten stars picOutput.Cls For i = 1 To 10 picOutput.Print "*"; Next i End Sub

Chapter 6 - Visual Basic Schneider 29 Example (request a number and display a row of that many stars) Private Sub cmdDisplay_Click() Dim i As Integer, stars As Integer ' Display a row of stars picOutput.Cls stars = Val(InputBox("Row length (1-20) : ")) For i = 1 To stars picOutput.Print "*"; Next i End Sub

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

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

Chapter 6 - Visual Basic Schneider 32 Example (display a 10 by 10 array of stars) Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer ' Display 10 x 10 square of stars For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub

Chapter 6 - Visual Basic Schneider 33 Guidelines for Choosing a Loop: If counting loop, use a For…… Next Loop. If trailer-values and body is executed at least once, use Do Until….. Loop. If trailer-value and nothing is known about the first execution, use Do While…. Loop. If you are not sure use Do While….. Loop.