Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Loop Blocks Chapter 6 Part A

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

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

4 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)

5 Do While..Loop Do while ( ) 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!

6 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.

7 Some Problems with Loops " off by one” error no exit condition (infinite loop - use Control-Break to stop!)

8 Example 1 intCount = 1 Do while (intCount < 10) intCount = intCount + 1 Loop MsgBox Str(intCount)

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

10 Example 3 intCount = 1 Do while (intCount <= 10) intCount = intCount + 1 Loop MsgBox Str(intCount)

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

12 Example 5 intCount = 1 Do while (intCount > 10) intCount = intCount + 1 Loop MsgBox Str(intCount)

13 Example 6 intCount = 1 Do intCount = intCount + 1 Loop While (intCount > 10) MsgBox Str(intCount)

14 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

15 The Interface

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

17 Algorithm Start "the number at 1“ Keep Looping increment "the number" While integer not divisible by "the number"

18 Code Dim intTestNum, intDivisor As Integer intTestNum = Val(txtInput.Text) intDivisor = 1 Do intDivisor = intDivisor + 1 Loop While (intTestNum Mod intDivisor <> 0)

19 Is it Prime? If (loop ended when intDivisor = intTestNum) the number is prime else its not

20 VB Code if (intDivisor = intTestNum) then MsgBox "It is Prime" Else MsgBox "It is NOT Prime" End If

21 Error Checking The main Do loop does not have to be executed if intTestNum <= 1 If < 1 prime NOT defined If = 1it is Prime by definition

22 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

23 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

24 Using Input Boxes InputBox Command InputBox, InputBox Function strName = InputBox (, ) Note the use of parentheses in function!

25 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)


Download ppt "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."

Similar presentations


Ads by Google