Unit V (Repetition)

CS 1130 Visual Basic—Fall 2017

Day 15 — Unit V Introduction


Review Unit IV Competency Demo

Repetition Concepts

We are getting close to the end of the "basics of computing"—actions & data, 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 data & actions and organizing the actions (on data). 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:

There are 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.

The for loop will initialize and increment the counter variable. You must do those yourself if you choose to use the do while or do until loop.

Roles of Variables

Our programs are now getting more complex. Before we address additional repetition examples I want to briefly describe the roles for which we most often use variables. The hope is that by mentioning these roles to you, the algorithms and programs that use them will make a bit more sense to you. The primary roles for variables are: (see The Roles of Variables Home Page for further info.)

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. So think carefully about whether something needs to occur to be ready for the next iteration of the loop code. If there some such code, it should most likely be placed just before the end of the loop.

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. You may keep repeating until the user enters a particular value or until a particular value is found or ... Just be sure to pay attention to the condition for stopping the loop.

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.


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:


Next Time

Day 16 — Unit V Work


Plans for Unit V

Now that we've had an introduction, I want to do a couple examples that involve processing things, i.e., the items in a list box the characters in a string. After that, you will have the rest of the period for working on your practice activity. Code for the examples done is included below.

The plan is that you will have tried to do all the practice activity before next class class period and bring any questions you have to class.

Next Time

Day 17 — Unit V Work


Today's Activities

The plan for today is to:  1) respond to any questions you have on the Unit V practice activity;  2) discuss a sample Unit V competency demo;  allow time to work on the practice activity

Next Time

Day 18 — Unit V Comp. Demo


Today's Activities

The plan for today is to:  1) respond to any questions you have on the Unit V practice activity;  and 2) take the competency demo for Unit V. If we have time after questioning, I will provide some information about arrays.

Next Time