Chapter 6 - Visual Basic Schneider

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
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.
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.
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 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 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.
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.
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.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
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.
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:
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 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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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 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 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
Programming Logic and Design Fourth Edition, Comprehensive
Topics Introduction to Repetition Structures
Iteration: Beyond the Basic PERFORM
Chapter 6 - VB.Net by Schneider
Topics Introduction to Repetition Structures
For...Next Statements.
Presentation transcript:

Chapter 6 - Visual Basic Schneider Repetition Chapter 6 - Visual Basic Schneider

Chapter 6 - Visual Basic Schneider Outline & Objectives Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops Chapter 6 - Visual Basic Schneider

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

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

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

The Do While ……. Loop Do While condition is true statement(s) Loop The statements between Do and Loop do not have to be indented. But indenting readability of the program As soon n as you see the word Do, your eyes can easily scan down the program to find the matching Loop statement. Chapter 6 - Visual Basic Schneider

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

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 The word While proclaims testing at the top. Output: 1 2 3 4 5 6 7 8 9 10 Chapter 6 - Visual Basic Schneider

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

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

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

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

Example for Counter-controlled loop num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Output: 1 2 3 4 5 6 7 8 9 10 Chapter 6 - Visual Basic Schneider

Example

Example Dim x As Integer Dim s As Integer Do While x >= 2 x = x + 1 s = s + x Loop picOutput.Print x; s

Example

Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop While x >= 2 picOutput.Print x; s

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

Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop Until x >= 2 picOutput.Print x; s

Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x Loop Until x <= 2 picOutput.Print x; s

Example Dim x As Integer Dim s As Integer Do Until x >= 2 x = x + 1 s = s + x Loop picOutput.Print x; s

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

Converting While loop to Until loop Do While Condition Action(s) Loop Do Until Not(Condition) Action(s) Loop Equivalents to Do While x <= y Action(s) Loop Do Until x > y Action(s) Loop Equivalents to Do While x <=3 and x >=10 Action(s) Loop Do Until Not(x<=3 and x>=10) Action(s) Loop Equivalents to

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

Review Which loop is infinite? i = 0 Do i = i + 1 Loop While i < 10 i = 1 Do While i < 10 i = i + 1 Loop NO NO i = 0 Do i = i + 10 Loop Until i < 10 i = 11 Do Until i < 10 Loop Yes Yes

EOF Function EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False Chapter 6 - Visual Basic Schneider

will be true if the end of file has been reached, and false otherwise Example will be true if the end of file has been reached, and false otherwise Dim count As Integer Open “DATA.TXT” For Input As #1 Do While NOT EOF(1) Input #1, x count= count + 1 s = s + x Loop Print count;s;x 5 150 50 The loop will repeated as long as the end of file is not reached 27 27

Counters and Accumulators 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 Schneider

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 COINS.TXT 50 10 5 25 Chapter 6 - Visual Basic Schneider

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

For … Next Loop 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 Schneider

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

picOutput.Print numVar; Next numVar Output: 1 3 5 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 Schneider

When a For statement is encountered The control variable is assigned the initial value. 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.) Iteration continues until the terminating value is exceeded. Chapter 6 - Visual Basic Schneider

Rules for Using For ... Next loop 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 Schneider

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 Schneider

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 Schneider

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

which of the following statements is true about the following code For i = 1 To 5.8 Step 1 pic1.Print i; Next I This code will print numbers from 1 to 6 This code will print numbers from 1 to 5 Run-time error This code will print numbers from 1 to 5.8

What is the output of the following code? Dim i As Integer For i = 9 To 7 Print i; Next i Nothing will be printed

What is the output of the following code? Dim i as integer i=4 for i=3 To 5 print 2*i; Next i print i 6 8 10 6

Example Dim x as integer x = 0 for x=3 To 1 print x; Next x print x 3

How many stars (*) are printed when the following code is executed? Dim i as Integer for i = 1 To 6 Step 1 print "*" i=i+2 next i 2 stars

Private Sub Command1_Click() For x = 1 To 3 Step 0 Print x; Next Print " finally "; x End Sub Infinite loop

Nested Loops For outer = 1 To 4 For inner = 1 To 2 .. Next inner Next outer Use indentation to improve readability In this example overtime the outer loop is executed once, the inner loop is twice Each loop must have a unique loop control variable. One loop is entirely contained within the outer loop Chapter 6 - Visual Basic Schneider

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

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

Review *** * ** ** * *** Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 3 For j = 1 To i 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 3 For j = i To 3 picOutput.Print "*"; Next j picOutput.Print Next i End Sub Display a 10by 10 square of stars Chapter 6 - Visual Basic Schneider

How many times the statement print "Good Luck" will be executed Dim i as integer, j as integer for i=0 To 3 for j=0 To 0 print "Good Luck" Next j Next i 4