Unit V (Repetition)

CS 1130 Visual Basic—Fall 2016

Day 18 — Unit V Introduction

Logistics

Review Unit IV Competency Demo

Repetition Concepts

We are getting close to the end of the "basics of computing"—actions, sequence, selection, iteration, and modularization. We keep finding out about more things we can use in the actions (controls and properties) and seeing ways to organize the actions. Again, the basics are actions and organizing the actions. Remember, the hardest part of programming is determining precisely what we want to accomplish.

Repetition is merely doing some task over and over again. For example:

Note that Chapter 14 of the text provides information about looping. It shows many different versions of loops. For us, it seems reasonable to concentrate on three of them. The For ... Next loop, the Do While... loop, and the Do Until ... loop.

Show equivalent examples that count to 10.

Planning and Implementing repetition

We must remember that the programming fundamental of sequence, must be considered for iteration to work correctly, you must carefully determine and place the actions necessary to accomplish your task. Some actions will need to be done before the repetition, some during the repetition, and some after the repetition. If you do not put all the actions in the right place, the overall result will not be correct.

Key idea # 1 in repetition—know precisely what you want to repeat

Clearly you have to tell the computer what you want it to repeat, DUH! Remember, the computer does what it is told to do—exactly what it is told and only what it is told. Be careful that you put only the actions you want repeated inside the loop. Some things should go before or after the loop.

Also, if something has to be done to be ready for the next repetition your code must include that. Sometimes (as in a For loop) getting ready for the next may be taken care of by the loop code itself. Even For loops, however, may still have to include actions to prepare for the next set of actions.

Key idea # 2 in repetition—determine when to stop repeating

Another critical consideration in iteration is determining when to stop or how many times to repeat something. If you want to process the items in a collection or lines in a file, you just keep processing until you've processed them all. Sometimes you can do that by using a counting loop and sometimes you don't know or cannot tell how many items there are and so must devise some sort of conditional expression to tell you when you are done. That leads to our next key idea.

Key idea # 3 in repetition—initialize

Your code has to include any and all the actions that need to come before the loop (ofter referred to as initialization). If you are counting things, the counter needs to be set to 0. If you are searching for a value, you have to get the value to be searched for. If you are checking for errors you will need to set an error flag to True or False before you start.

Key idea # 4 in repetition—after the loop

Finally, there is almost always some action that needs to be done after the loop. Reporting the result from the repetition is usually called for. Sometimes repetition had two tests for determining when to stop, e.g., look at the items to see if a particular value is included. In this case we would want to stop when we found it or when we had looked at all the values. After the loop, we know we stopped but we don't know why we stopped. We will need to test (using an If) to see why we stopped and take the appropriate action.

Examples

We are going to show some examples that illustrate the key ideas and the Visual Basic code for iteration. We will use the idea of coin flipping simulations in all of them. The plan for each is to:

The examples are:

o

Next Time

Day 19 — Unit V Work

Logistics

Project Discussion

Many of you have discussed possible projects with me and or submitted projects. Several have had their official project proposal meetings with me and several more are scheduled for this week. If you are not one of those people, you really need to start thinking about your project.

The original goal was that you would have met with me by last Friday. The next goal is to meet with me this week. If you can't make that, be sure you get your proposal in and meet with me early next week. The late penalty will grow (half a point this week, 1 point next week, 2.5 the next ...).

oct

Unit V Questions

Next Time

Day 20 — Files;   Unit V Work

Logistics

Using Files

There are four steps in using a file—declare a variable for accessing the file; open the file; use the file (read from or write to it); close the file. Those are discussed in more detail below.

  1. declare a file

    Files have internal and external names. External names are what we seen in our folders. They also contain the "path" information. In VB, there is a default and easy to use location for files — the bin/Debug folder. I suggest you always use that (in our class at least). Otherwise, your program may not work if it is run on someone else's computer.

    Declaring a file typically includes preparing variables for the internal file name and the external file name. The internal name will be a StreamReader or a StreamWriter depending on whether you are reading from or writing to a file. We will be reading from a file. The external file name is a string that contains a name for it (it can be a variable or a literal, we'll use a variable). See code for all this below.

  2. open the file

    Opening a file for reading uses the .OpenText() method (as shown below). Opening a file for writing use the .CreateText() method (shown below) when you want a new file or to replace an existing file. If you want to add text to a file instead of creating a new file, use the .AppendText() method. All these methods require that you supply the file name as a parameter to the method.

  3. use the file

    Using the file means reading data from it (into some variable) or writing data to it from a variable or by building the string you want to put in the file. Keep in mind that the file contains text, usually lines of text. When reading a file if you attempt to read numbers, VB will convert the string values to numbers according to its rules. I assume numbers would also be converted to text on output.

  4. close the file

    When you are done reading or writing the file, you should always close it. The .Close() method works for both files being read from and files being written to.

The "Text-Tools" use of file for input

As noted above we need to declare the file and

    '........... Declare variables
    Dim textReader As IO.StreamReader
    Dim fileName, dataIn As String

    '........... Open the file
    fileName = inputBox("Name of file?")
    fileLine = textReader.ReadLine()

    '........... Read and analyze lines
    Do until VB.EndOfStream()
    	dataIn = textReader.Readline
    	'         code to process the line of data
    Loop
    
    textReader.Close()

Questions? Comments? Wonderings?

Counting the word lengths

Last time I showed you (on the board) how you can use an array to count the various dice roll values. You will need to use a similar process here. The idea is to create an array of counts to store the word lengths and when you find a word increment the appropriate count. The code below shows this along with a subroutine for initializing the counts.

Public Class textTools
    '........... class variables
    Dim wordCounts(15) as Integer
    
    Private Sub InitializeCounts()
    	For i as Integer = 1 to 15
    	    wordCounts(i) = 0
    	Next
    End Sub


    '........... Declare variables
    Dim textReader As IO.StreamReader
    Dim fileName, dataIn As String
    
    InitializeCounts()

    '........... Open the file
    fileName = inputBox("Name of file?")
    textReader = IO.File.OpenText(fileName)

    '........... Read and analyze lines
    Do until VB.EndOfStream()
    	dataIn = textReader.Readline
    	'         code to process the line of data
    Loop
    
    textReader.Close()

Unit V Questions

Next Time

Day 21 — Unit V Work

Logistics

Unit V Questions

Discuss the looping elements of the coin flips

Class works on the items

Next Time

Day 22 — Unit V Work

Logistics

Unit V Questions

Discuss the looping elements of the flash card and text-analysis items

Next Time