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.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
CS0004: Introduction to Programming Repetition – Do Loops.
Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
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 61 Post Test Loop Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run.
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 61 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Processing Lists of Data with Do Loops (Examples)
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Lecture Set 5 Control Structures Part D - Repetition with 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.
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.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
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 6: The Repetition Structure
Visual Basic Programming
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
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.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.
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. Chapter 5: Loops.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.
Sum of Arithmetic Sequences. Definitions Sequence Series.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
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.
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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
UNIT 5 Lesson 15 Looping.
Chapter 7 Arrays.
CiS 260: App Dev I Chapter 4: Control Structures II.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Programming Fundamentals
Introducing Do While & Do Until Loops & Repetition Statements
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Control Structures - Repetition
Objectives After studying this chapter, you should be able to:
Chapter 6 - VB.Net by Schneider
Prepared By: Deborah Becker
For...Next Statements.
REPETITION Why Repetition?
Presentation transcript:

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 1. Do Loops 2. For…Next loops. n A Do Loop repeats a sequence of statements either as long as or until a certain condition is true. n A Do statement proceeds the sequence of statements, and a loop statement follows the sequence of statement. n The condition, along with either the word While or Until, follows the word Do or the word Loop.

Chapter 72 Repetition - Do Loops n Do loop of the form Do While condition statement(s) Loop It first checks the truth value of condition. If condition is false, then the statements inside the loop are not executed n The program continues with the line after the Loop statement. n If condition is true, then the statements inside the loop are executed. n The entire process is repeated, beginning with the test of condition in the Do While statement, In other worlds, the statements inside the loop are repeatedly executed only as long as the condition is true.

Chapter 73 Repetition - Do Loops n Flowchart for a Do While Loop.

Chapter 74 Repetition - Do While Loops n Example 1 (p251) n The following segment 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 75 Repetition - Do While Loops n Example 2 - p.251 Required user gives password before accessing a file n Private Sub cmdDisplay_Click() Dim passWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then passWord = "" Do While passWord <> "SHAZAM" passWord = InputBox("What is the password?") passWord = UCase(passWord) Loop End If Open App.Path & "\" & txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1 End Sub

Chapter 76 Repetition - Do Until Loops n In examples 1 and 2 the condition was checked at the top of the loop- that is, before the statements were executed. n Alternatively, the condition can be checked at the bottom of the loop when the statement Loop is reached. Do statement(s) Loop Until condition n It executes the statements inside the loop and then checks the truth value of condition. If condition is true, then the program continues with the line after the loop statement. n If condition is false, then the entire process is repeatedly executed until condition is true.

Chapter 77 Repetition - Do Until Loops n Flowchart Of Do Until Loop

Chapter 78 Repetition - Do Until Loops n The following program is equivalent to Example 2, except that the condition is tested at the bottom of the loop (p253) 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 App.Path & "\" & txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1 End Sub

Chapter 79 List of Data with Do loops n One of the main applications of programming is the processing of lists of data from a file n Data to be processed are often retrieved from a file by a Do loop. n EOF function, Counters, flags, accumulators and Nest loops are useful function in Visual Basic

Chapter 710 EOF function n Suppose a file has been opened with reference number n, At any time, the condition EOF(n) will be true if the end of the file has been reached, and false otherwise. n Example (p.263) Private Sub cmdDisplay_Click() Dim nom As String, phoneNum As String picNumbers.Cls Open App.Path & "\PHONE.TXT" For Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picNumbers.Print nom, phoneNum Loop Close #1 End Sub

Chapter 711 EOF function n Flowchart for processing data from a file

Chapter 712 Counter & Accumulator n A counter is a numeric variable that keeps track of the number of items that have been processed. n An accumulator is a numeric variable that totals number. n Example (p.265 Ex.23) n Do While Not EOF(1) numCoins=numCoins Counter sum= sum+value Accumulator n Loop

Chapter 713 Flag n A flag is variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is the Boolean data type. n Explain Example (p.266 ex. 4) The flag variable called order flag, is initially assigned the Value True an is set to False if a pair of adjacent words is out of order.

Chapter 714 Nested Loop n Statements inside of Do loop can consist of another Do loops. n Such a configuration is referred to as nested loops and is useful in repearting a single data-processing routine several times. n Explain Example: (page 267 ex5) DO While ( ) Do While ( ) Loop Loop

Chapter 715 Exercises n Ex 6.1 no. 3, 4, 7, 21, 23 n Ex 6.2 no. 1, 5, 21