Chapter 6
Part A

Program Blocks

  1. Actions- commands, messages, methods
  2. Branches- decisions to be made
  3. Loops- actions to be repeated

Loop blocks

Loops have 4 parts

  1. a beginning
  2. an end
  3. body
  4. test (Boolean)

Two Types of Loops

  1. When you know how many times to execute (definite)
  2. When you keep executing until the user says to stop (indefinite)

Do While..Loop 

Do While (Boolean expression) 'beginning of the loop AND the test
 do something
 do something
 Want to do it again?
Loop
'the body of the loop

'the end of the loop

Notice that the test is at the beginning of the loop. This means that the commands in the loop may never be executed! 

Do.. Loop While

Do 'beginning of the loop 
 do something
 do something
 Want to do it again?
Loop While (Boolean expression)
'the body of the loop

'the end of the loop AND the test

Notice that the test is at the end of the loop. This means that the commands in the loop must be executed at least once.

Some problems with loops

bullet "off by one" error
bullet no exit condition (infinite loop - use Control-Break to stop!)
intCount = 1
Do while (intCount < 10)

  intCount = intCount + 1

Loop

MsgBox Str(intCount)

intCount = 1
Do 

  intCount = intCount + 1

Loop While (intCount < 10)

MsgBox Str(intCount)

intCount = 1
Do while (intCount <= 10)

  intCount = intCount + 1

Loop

MsgBox Str(intCount)

intCount = 1
Do 
  intCount = intCount + 1

Loop While (intCount <= 10)

  MsgBox Str(intCount)

intCount = 1
Do while (intCount > 10)

  intCount = intCount + 1

Loop

MsgBox Str(intCount)

intCount = 1
Do 

  intCount = intCount + 1

Loop While (intCount > 10)

MsgBox Str(intCount)

Class Exercise

Write a program that allows the user to input an integer into a text box and test the integer to see if it is a prime number. Prime numbers are numbers that are only divisible by 1 and themselves. After clicking a Check button, the program uses a message box to tell the user if the number is Prime or Not Prime (Note: the book uses a label rather that a message box!)

The Interface is straightforward.

The objects are:

bullet frmPrime- the form
bullet txtInput- text box for input
bullet cmdCheck
bullet cmdEnd
bullet lblEnterIntegerLabel

The Code

Before coding, we need to determine how to solve this problem (the algorithm). One way to solve this problem is to divide the integer by every number from 2 to that number (There are faster ways for humans to do it, but the computer has no problem doing a bunch of redundant calculations.) We will continue to divide until we find a number that divides it evenly (Integer MOD Number = 0). Another way of saying this is "We will keep looping while the integer divided by the number is NOT 0- Integer MOD Number < > 0" Thus, the algorithm of the main loop is

Start "the number at 1"
Keep Looping

increment "the number"

While the integer is not divisible by "the number"

in VB

Dim intTestNum, intDivisor As Integer
intTestNum = Val(txtInput.Text)
intDivisor = 1
Do

intDivisor = intDivisor + 1

Loop While (intTestNum Mod intDivisor <> 0)

It is important to note that this loop ALWAYS ends (eventually intDivisor will evenly divide intTestNum). Now, how do we know if the number is prime or not. Well, if the loop kept executing until intDivisor = intTestNum, the number is prime. In other words, if no other divisors were found, the number is prime.

If (the loop ended only when intDivisor = intTestNum) then

the number is prime

else

its not

In VB

if (intDivisor = intTestNum) then

MsgBox "It is Prime"

Else

MsgBox "It is NOT Prime"

End If

Lets take another look at the primary loop.

Do

intDivisor = intDivisor + 1

Loop While (intTestNum Mod intDivisor <> 0)

It works fine, except for one thing. I ASSUMES that the user typed in a positive number (a number > 0). Primes are not defined for 0 and negative numbers. Programmers should NOT make such assumptions. One way to make sure that the number entered in the text box is > 0 is to use the IF statement to trap numbers < 1. Also, we do not need to execute the loop if the user entered a 1 (because intTestNumber = 1 and intDivisor = 1, we can skip the loop). Therefore, we will make sure the user types a a number greater that 1. We can then put the loop in the If portion and an error message in Else. 

If ("the test number" > 1) 

execute the loop

Else

give an error message

in VB

If (intTestNumber > 1) then

Do

intDivisor = intDivisor + 1

Loop While (intTestNum Mod intDivisor <> 0)

Else

MsgBox "Please Enter a positive integer"

End If

Using Input Boxes

Input boxes are a little like message boxes in that they are created at runtime and can be used to communicate with the user. The difference is that an input box has a text box on it which allows the user to type into. To display an input box, use the following code

InputBox <message prompt>, <caption>

where the message prompt is a string to be displayed and the caption is a string to be displayed on the title bar. Thus the command

InputBox "What is your name?" , "This is an Input box"

produces:

If you want to "remember" what the user type in, you can assign it to a variable. However, you must use a different version. The InputBox keyword we learned above is called a command- it tells the computer to do something. To have the InputBox return the value, we must use InputBox function.

strYourName = InputBox(<message prompt>, <caption>)

Notice that the differences

bullet

in the function, the keyword InputBox is on the right hand of an equal sign

bullet

the message prompt and caption are inside parentheses

Thus the program

Private Sub Form_Load()

Dim strYourName As String
strYourName = InputBox("What is your name?", "This is an Input box")
MsgBox "Hello " + strYourName

End Sub

Produces the following input box

And the following message box

Homework

Modify Review 2 (page 6-2) to have the user to input the integer using a horizontal scroll bar rather that a text box

Do Review 4a & 4b (page 6-8)

Exercises 1, 2, and 3 (a & b)