Loop Blocks - Distance Learning 101

Download Report

Transcript Loop Blocks - Distance Learning 101

Loop Blocks
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>)
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 1
intCount = 1
Do while (intCount < 10)
intCount = intCount + 1
Loop
MsgBox Str(intCount)
Example 2
intCount = 1
Do
intCount = intCount + 1
Loop While (intCount < 10)
MsgBox Str(intCount)
Example 3
intCount = 1
Do while (intCount <= 10)
intCount = intCount + 1
Loop
MsgBox Str(intCount)
Example 4
intCount = 1
Do
intCount = intCount + 1
Loop While (intCount <= 10)
MsgBox Str(intCount)
Example 5
intCount = 1
Do while (intCount > 10)
intCount = intCount + 1
Loop
MsgBox Str(intCount)
Example 6
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
The Interface
The Objects
• frmPrime- the form
• txtInput- text box for input
• cmdCheck
• cmdEnd
• lblEnterIntegerLabel
Algorithm
Start "the number at 1“
Keep Looping
increment "the number"
While integer not divisible by
"the number"
Code
Dim intTestNum, intDivisor As Integer
intTestNum = Val(txtInput.Text)
intDivisor = 1
Do
intDivisor = intDivisor + 1
Loop While (intTestNum Mod intDivisor <> 0)
Is it Prime?
If (loop ended when
intDivisor = intTestNum)
the number is prime
else
its not
VB Code
if (intDivisor = intTestNum) then
MsgBox "It is Prime"
Else
MsgBox "It is NOT Prime"
End If
Error Checking
The 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 Blocks
We 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
Else
give an error message
VB Code
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
InputBox Command
InputBox <message prompt>, <caption>
InputBox Function
strName = 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)