CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
5.05 Apply Looping Structures
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.
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.
Chapter 12: How Long Can This Go On?
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.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
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 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
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 -
Introduction to Problem Solving and Control Statements.
Repetition Structures
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
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.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
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.
UNIT 5 Lesson 15 Looping.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Visual Basic 6 (VB6) Data Types, And Operators
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
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Introduction to Problem Solving and Control Statements
Fundamentals of visual basic
Case & Repetitive Statements
Chapter 6 - VB.Net by Schneider
Topics Introduction to Repetition Structures
Prepared By: Deborah Becker
For...Next Statements.
Presentation transcript:

CHAPTER SIX LOOPS © Prepared By: Razif Razali 1

FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which one is better? State your reasons 2 © Prepared By: Razif Razali

CONTENTS While For Do/Loop While and Do While/Loop Do/Loop Until and Do Until/Loop 3 © Prepared By: Razif Razali

I NTRODUCTION Visual Basic allows a procedure to be repeated many times as long as the processor until a condition or a set of conditions is fulfilled. This is generally called looping. The process of repeating a series of statements multiple times until a criteria is met Looping is a very useful feature of Visual Basic because it makes repetitive works easier. 4 © Prepared By: Razif Razali

R EPETITION S TRUCTURES Visual Basic provides six types of repetition structures for performing a statement or group of statements repeatedly: While...Wend For...Next Do While...Loop Do Until...Loop Do...Loop While Do...Loop Until 5

W HILE..W END S TATEMENT If condition is True, all statements in statements are executed until the Wend statement is encountered. Control then returns to the While statement and condition is again checked. If condition is still True, the process is repeated. If it is not True, execution resumes with the statement following the Wend statement. 6 © Prepared By: Razif Razali

W HILE... W END S TATEMENT A While...Wend statement behaves like the Do While...Loop statement. The following While...Wend counts from 1 to 10 Dim number As Integer number = 1 While number <=10 Print number number = number + 1 Wend 7 © Prepared By: Razif Razali

E XERCISE 1 Determine the output for the following VB program 8 © Prepared By: Razif Razali

F OR..N EXT L OOP With a For...Next loop, you can execute a specific group of program statements in an event procedure a specific number of times. This can be useful when you want to perform several related calculations, work with elements on the screen, or process several pieces of user input. 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 9 © Prepared By: Razif Razali

E XAMPLE 1: F OR..N EXT 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 10 © Prepared By: Razif Razali

E XAMPLE 2: F OR..N EXT Dim numVar As Integer For numVar = 1 To 5 Step 2 picTable.Print numVar; Next numVar 11 © Prepared By: Razif Razali

W HEN A F OR 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. Each For loop must end with a Next statement 12 © Prepared By: Razif Razali

E XAMPLE 3: F OR..N EXT Private Sub cmdDisplay_Click() Dim i As Integer For i = 1 To 10 picOutput.Print "*"; Next i End Sub 13 © Prepared By: Razif Razali

E XAMPLE 4: F OR..N EXT Dim numVar As Integer For numVar = 8 To 1 Step -2 picOutput.Print numVar; Next numVar 14 © Prepared By: Razif Razali

N ESTED L OOPS In some cases, it is convenient to use a loop contained within another loop. Such loops are called nested loops. The syntax nested for loop For outer = 1 To 4 ` The outer loop For inner = 1 To 2 ` the inner loop.. Next inner Next outer Notice that all statements in the inner loop are contained within the boundaries of the outer loop. 15 © Prepared By: Razif Razali

E XAMPLE 5: D ISPLAY A 10 X 10 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 16 © Prepared By: Razif Razali

E XERCISE 2 By using the for loops, write a VB statement to have the following output: 17 © Prepared By: Razif Razali

U SING THE EXIT FOR S TATEMENT Use an Exit For statement to exit a For...Next loop before the loop has finished executing. With this capability, you can respond to specific events that occur before the loop runs the preset number of times. For example, in this For...Next loop, the loop prompts the user for 10 names and prints them on the form, unless the user enters the word Done. (If the user does enter Done, the program jumps to the first statement that follows the Next statement): 18 © Prepared By: Razif Razali

U SING THE EXIT FOR S TATEMENT For i = 1 To 10 InpName = InputBox("Type a name or Done to quit.") If InpName = "Done" Then Exit For Print InpName Next i As this example shows, you can use If statements with Exit For statements. You’ll find this combination useful for handling special cases that come up in a loop, and you'll probably use it often. 19 © Prepared By: Razif Razali

U SING THE EXIT FOR S TATEMENT You can include any number of Exit For statements anywhere in a For loop. One use of Exit For is to test for a condition that could cause an endless loop, which is a loop that could run a very large or even infinite number of times. If you detect such a condition, you can use Exit For to escape the loop. 20 © Prepared By: Razif Razali

W RITING D O L OOPS Do loops are valuable because occasionally you can’t know in advance how many times a loop should repeat. As an alternative to a For...Next loop, you can write a Do loop that executes statements until a certain condition in the loop is true. For example, you might want to let the user enter names in a database until the user types Done in an input box. In that case, you could use a Do loop to cycle indefinitely until the user enters the text string, Done. 21 © Prepared By: Razif Razali

T HE D O L OOP The formats are a) Do While condition Block of one or more VB statements Loop b) Do Block of one or more VB statements Loop While condition c) Do Until condition Block of one or more VB statements Loop d) Do Block of one or more VB statements Loop Until condition 22 © Prepared By: Razif Razali

D O W HILE L OOP Is executed as long as the condition is True. If condition is False then the next statement after the Loop is executed. Do While Syntax Do While condition is true statement(s) Loop 23 © Prepared By: Razif Razali

E XAMPLE 1 : D O W HILE 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 24 © Prepared By: Razif Razali

C ONTROLLING L OOPS 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. 25 © Prepared By: Razif Razali

C OUNTER - CONTROLLED L OOPS 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. 26 © Prepared By: Razif Razali

E XAMPLE : R EPEAT R EQUEST AS L ONG AS R ESPONSE IN I NCORRECT Dim passWord As String = "" Do While passWord <> “KPTM" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop 27 Chapter 6 - VB 2008 by Schneider passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop.

D O U NTIL L OOP Is executed until the condition becomes True Any Do While…. Loop can be rewritten as a Do Until….. Loop Syntax Do statement(s) Loop Until condition 28 Chapter 6 - VB 2008 by Schneider

E XAMPLE 1: D O L OOP U NTIL Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = “KPTM" 29 Chapter 6 - VB 2008 by Schneider

C OMPARING W HILE … AND U NTIL L OOPS 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 30 © Prepared By: Razif Razali

D O...L OOP W HILE S TATEMENT The Do...Loop While statement first executes the statements and then test the condition after each execution. The following program block illustrates the structure: Dim number As Long number = 0 Do number = number + 1 Loop While number < 201 The programs executes the statements between Do and Loop While structure in any case. Then it determines whether the counter is less than 201. If so, the program again executes the statements between Do and Loop While else exits the Loop. 31 © Prepared By: Razif Razali

I NFINITE L OOPS An example of an infinite loop: Dim Count As Integer Count = 1 while Count <= 25 Print Count Count = Count - 1; Wend This loop will continue executing until interrupted (Control-C) or until an underflow error occurs 32 © Prepared By: Razif Razali

SUMMARY A loop allows a statement or series of statements to be repeated. Do..Loop continue to execute the statements in the loop until a condition is met. Do..Loop can have the condition test at the top or the bottom of the loop and can use a While or Until to test the condition. A Do..Loop can be used to locate a selected item in a combo box. 33 © Prepared By: Razif Razali