DOC PREVIEW
Chapter 6 – Repetition

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 6 – Repetition6.1 Do LoopsDo Loop SyntaxPseudocode and Flow Chart for a Do LoopExample 1Example 2Post Test LoopExample 3Pseudocode and Flowchart for a Post-Test LoopComments6.2 Processing Lists of Data with Do LoopsTerminologyPeek MethodPeek ExampleSlide 15Pseudocode and Flow Chart for Processing Data from a FileSlide 17Counters and AccumulatorsSlide 19FlagsExample 4Nested LoopsExample 5More About FlagsFlags continued6.3 For…Next LoopsSampleDo While equivalentFor…Next Loop SyntaxSlide 30Slide 31Slide 32Nested For…Next LoopsSlide 34Example 4 OutputFor and Next PairsStart, Stop, and Step valuesAltering the Control VariableNon-integer Step ValuesNon-integer Step ExampleChapter 6 - VB.Net by Schneider 1Chapter 6 – Repetition•6.1 Do Loops•6.2 Processing Lists of Data with Do Loops•Peek Method •Counters and Accumulators •Flags•Nested Loops•6.3 For...Next Loops•6.4 A Case Study: Analyze a LoanChapter 6 - VB.Net by Schneider 26.1 Do Loops•A loop is one of the most important structures in programming.•Used to repeat a sequence of statements a number of times. •The Do loop repeats a sequence of statements either as long as or until a certain condition is true.Chapter 6 - VB.Net by Schneider 3Do Loop SyntaxDo While condition statement(s)LoopCondition is tested,If it is True, the loop is run.If it is False, the statements following the Loop statementare executed. These statements are inside the body of the loop and are run if the condition above is True.Chapter 6 - VB.Net by Schneider 4Pseudocode and Flow Chart for a Do LoopChapter 6 - VB.Net by Schneider 5Example 1Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num LoopEnd SubChapter 6 - VB.Net by Schneider 6Example 2passWord = ""Do While passWord <> "SHAZAM" passWord = InputBox("What is the password?") passWord = passWord.ToUpperLooppassWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop.Chapter 6 - VB.Net by Schneider 7Post Test LoopDo statement(s)Loop Until conditionLoop is executed once and then the conditionis tested. If it is False, the loop is run again.If it is True, the statements following the Loop statement are executed.Chapter 6 - VB.Net by Schneider 8Example 3Do passWord = InputBox("What is the password?") passWord = passWord.ToUpperLoop Until passWord = "SHAZAM"Chapter 6 - VB.Net by Schneider 9Pseudocode and Flowchart for a Post-Test LoopChapter 6 - VB.Net by Schneider 10Comments•Be careful to avoid infinite loops – loops that never end.•VB.NET allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop.•(This text will use only While at the top and only Until at the bottom.)Chapter 6 - VB.Net by Schneider 116.2 Processing Lists of Data with Do Loops•Display all or selected items from lists•Search lists for specific items•Perform calculations on the numerical entries of a listChapter 6 - VB.Net by Schneider 12Terminology•Counters calculate the number of elements in lists•Accumulators sum numerical values in lists•Flags record whether certain events have occurred•Peek method can be used to determine when the end of a text file has been reachedChapter 6 - VB.Net by Schneider 13Peek Method•Data to be processed are often retrieved from a file by a Do loop•To determine if we have reached the end of the file from which we are reading, use PeekChapter 6 - VB.Net by Schneider 14Peek Example •a file has been opened as a StreamReader object named sr. •sr.Peek is the ANSI value of the first character of the line about to be read with ReadLine. If the end of the file has been reached, the value of sr.Peek is -1Chapter 6 - VB.Net by Schneider 15Example 1Dim sr As IO.StreamReader = _ IO.File.OpenText("PHONE.TXT")lstNumbers.Items.Clear()Do While sr.Peek <> -1 name = sr.ReadLine phoneNum = sr.ReadLine lstNumbers.Items.Add(name & " " _ & phoneNum)Loopsr.Close()Chapter 6 - VB.Net by Schneider 16Pseudocode and Flow Chart for Processing Data from a FileChapter 6 - VB.Net by Schneider 17Example 2Do While (name <> txtName.Text) _ And (sr.Peek <> -1) name = sr.ReadLine phoneNum = sr.ReadLineLoopAs long as the name being searched for has not been found AND the end of the file has not been reached, the loop will continueChapter 6 - VB.Net by Schneider 18Counters and Accumulators•A counter is a numeric variable that keeps track of the number of items that have been processed. •An accumulator is a numeric variable that totals numbers.Chapter 6 - VB.Net by Schneider 19Example 3numCoins = 0sum = 0Do While sr.Peek <> -1 coin = sr.ReadLine numCoins += 1 sum += CDbl(coin) LoopnumCoins is a counter, it increases by 1 each time through the loopSum is an accumulator. It is used to total up the valuesof the coins.Chapter 6 - VB.Net by Schneider 20Flags•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 - VB.Net by Schneider 21Example 4Do While (sr.Peek <> -1) word2 = sr.ReadLine wordCounter += 1 If word1 > word2 Then orderFlag = False End If word1 = word2LoopChapter 6 - VB.Net by Schneider 22Nested LoopsStatements inside a loop can contain another loop.Chapter 6 - VB.Net by Schneider 23Example 5Do While (foundFlag = False) And (sr1.Peek <> -1) fileName = sr1.ReadLine Dim sr2 As IO.StreamReader = _ IO.File.OpenText(fileName) Do While (name <> txtName.Text) And _ (sr2.Peek <> -1) name = sr2.ReadLine phoneNum = sr2.ReadLine Loop sr2.Close() If name = txtName.Text Then txtNumber.Text = name & " " & phoneNum foundFlag = True End IfLoopOuterloopInnerloopChapter 6 - VB.Net by Schneider 24More About Flags•When flagVar is a variable of Boolean type, the statementsIf flagVar = True ThenandIf flagVar = False Then•can be replaced byIf flagVar Then and If Not flagVar ThenChapter 6 - VB.Net by Schneider 25Flags continued•The statementsDo While flagVar = True and Do While flagVar = False•can be replaced byDo While flagVar and Do While Not flagVarChapter 6 - VB.Net by Schneider 266.3 For…Next Loops•Used when we know how many times we want the loop to execute•A counter controlled loopChapter 6 - VB.Net by Schneider 27SampleFor i = 1 To 5


Chapter 6 – Repetition

Download Chapter 6 – Repetition
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 6 – Repetition and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 6 – Repetition 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?