Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
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
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
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.
ENGR 112 Decision Structures.
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
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 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.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
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
Topics Introduction to Repetition Structures
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.
Iteration: Beyond the Basic PERFORM
Case & Repetitive Statements
Chapter 6 - VB.Net by Schneider
Topics Introduction to Repetition Structures
For...Next Statements.
Presentation transcript:

Repetition Chapter 6 - Visual Basic Schneider 1

 Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 - Visual Basic Schneider2

 Do While ……. Loop  Do Until …… Loop  For …… Next loop Chapter 6 - Visual Basic Schneider3

 Looping: the process of repeating a series of statements multiple times until a criteria is met Chapter 6 - Visual Basic Schneider4

 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 Schneider5

Do While condition is true statement(s) Loop Chapter 6 - Visual Basic Schneider6

7 Is the condition true Execute statements within the loop Execute statements that follow the loop Yes No

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 Schneider8

 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 Schneider9

 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 Schneider10

passWord = "" Do While passWord <> "SHAZAM" passWord = UCase(InputBox("What is the password?")) Loop Chapter 6 - Visual Basic Schneider11

 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 Schneider12

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

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

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 Schneider15

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 Schneider16

 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 Schneider17

 EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False  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 Schneider18

 A counter is a numeric variable that keeps track of the number of items that have been processed in a loop.  An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop. Chapter 6 - Visual Basic Schneider19

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

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

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?

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

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

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 Chapter 6 - Visual Basic Schneider25 Initial Value Control variable Terminating value

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

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 Schneider27

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

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

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 Schneider30

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

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

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

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;

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