loop blocks chapter 6 part a. program blocks 1.actions- commands, messages, methods 2.branches-...
DESCRIPTION
Loop Blocks Loops have 4 parts 1.a beginning 2.an end 3.body 4.test (Boolean)TRANSCRIPT
Loop Blocks Chapter 6
Part A
Program Blocks 1.Actions- commands,
messages, methods2.Branches- decisions to
be made3.Loops- actions to be
repeated
Loop Blocks Loops have 4 parts
1.a beginning2.an end3.body4.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>) do something
do something Want to do it again?
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
do something do something Want to do it again?
Loop While (Boolean expression)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
• "off by one” error•no exit condition (infinite loop - use Control-Break to stop!)
Example 1intCount = 1Do while (intCount < 10)
intCount = intCount + 1Loop
MsgBox Str(intCount)
Example 2intCount = 1Do intCount = intCount + 1Loop While (intCount < 10)MsgBox Str(intCount)
Example 3intCount = 1Do while (intCount <= 10)
intCount = intCount + 1LoopMsgBox Str(intCount)
Example 4intCount = 1Do
intCount = intCount + 1 Loop While (intCount <= 10)
MsgBox Str(intCount)
Example 5intCount = 1Do while (intCount > 10)
intCount = intCount + 1Loop
MsgBox Str(intCount)
Example 6intCount = 1Do
intCount = intCount + 1Loop 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
The Interface
The Objects •frmPrime- the form•txtInput- text box for input•cmdCheck•cmdEnd•lblEnterIntegerLabel
AlgorithmStart "the number at 1“Keep Looping
increment "the number"While integer not divisible by
"the number"
CodeDim intTestNum, intDivisor As IntegerintTestNum = Val(txtInput.Text)intDivisor = 1Do
intDivisor = intDivisor + 1Loop While (intTestNum Mod intDivisor <>
0)
Is it Prime?If (loop ended when intDivisor = intTestNum)
the number is primeelse
its not
VB Codeif (intDivisor = intTestNum) then
MsgBox "It is Prime"Else
MsgBox "It is NOT Prime"End If
Error CheckingThe main Do loop does not
have to be executed if intTestNum <= 1
•If < 1 prime NOT defined•If = 1 it is Prime by definition
Nesting BlocksWe will place the main DO loop inside
an If..Then so that it is ONLY executed if intTestNum > 1. Otherwise we will give an error message
If ("the test number" > 1) execute the loop
Elsegive an error message
VB CodeIf (intTestNumber > 1) then
DointDivisor = intDivisor + 1
Loop While (intTestNum Mod intDivisor <> 0)
ElseMsgBox "Please Enter a positive integer"
End If
Using Input Boxes InputBox CommandInputBox <message prompt>, <caption>
InputBox FunctionstrName = InputBox (<message prompt>,
<caption>)
Note the use of parentheses in function!
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 4, a & b (page 6-8)• Exercises 1, 2, and 3 (a & b)