loop blocks chapter 6 part a. program blocks 1.actions- commands, messages, methods 2.branches-...

25
Loop Blocks Chapter 6 Part A

Upload: kristina-hodges

Post on 20-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Loop Blocks Loops have 4 parts 1.a beginning 2.an end 3.body 4.test (Boolean)

TRANSCRIPT

Page 1: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Loop Blocks Chapter 6

Part A

Page 2: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Program Blocks 1.Actions- commands,

messages, methods2.Branches- decisions to

be made3.Loops- actions to be

repeated

Page 3: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Loop Blocks Loops have 4 parts

1.a beginning2.an end3.body4.test (Boolean)

Page 4: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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)

Page 5: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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! 

Page 6: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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.

Page 7: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Some Problems with Loops

• "off by one” error•no exit condition (infinite loop - use Control-Break to stop!)

Page 8: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Example 1intCount = 1Do while (intCount < 10)  

  intCount = intCount + 1Loop

MsgBox Str(intCount)

Page 9: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Example 2intCount = 1Do    intCount = intCount + 1Loop While (intCount < 10)MsgBox Str(intCount)

Page 10: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Example 3intCount = 1Do while (intCount <= 10)  

intCount = intCount + 1LoopMsgBox Str(intCount)

Page 11: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Example 4intCount = 1Do 

  intCount = intCount + 1 Loop While (intCount <= 10)

MsgBox Str(intCount)

Page 12: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Example 5intCount = 1Do while (intCount > 10)  

intCount = intCount + 1Loop

MsgBox Str(intCount)

Page 13: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Example 6intCount = 1Do   

intCount = intCount + 1Loop While (intCount > 10)

MsgBox Str(intCount)

Page 14: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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

Page 15: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

The Interface

Page 16: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

The Objects •frmPrime- the form•txtInput- text box for input•cmdCheck•cmdEnd•lblEnterIntegerLabel

Page 17: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

AlgorithmStart "the number at 1“Keep Looping

increment "the number"While integer not divisible by

"the number"

Page 18: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

CodeDim intTestNum, intDivisor As IntegerintTestNum = Val(txtInput.Text)intDivisor = 1Do

intDivisor = intDivisor + 1Loop While (intTestNum Mod intDivisor <>

0)

Page 19: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Is it Prime?If (loop ended when intDivisor = intTestNum)

the number is primeelse

its not

Page 20: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

VB Codeif (intDivisor = intTestNum) then

MsgBox "It is Prime"Else

MsgBox "It is NOT Prime"End If

Page 21: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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

Page 22: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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

Page 23: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

VB CodeIf (intTestNumber > 1) then

DointDivisor = intDivisor + 1

Loop While (intTestNum Mod intDivisor <> 0)

ElseMsgBox "Please Enter a positive integer"

End If

Page 24: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

Using Input Boxes InputBox CommandInputBox <message prompt>, <caption>

InputBox FunctionstrName = InputBox (<message prompt>,

<caption>)

Note the use of parentheses in function!

Page 25: Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions…

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)