Presentation is loading. Please wait.

Presentation is loading. Please wait.

110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:

Similar presentations


Presentation on theme: "110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:"— Presentation transcript:

1 110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops: Do Loops Do While/Loop, Do/Loop While Do Until/Loop, Do/Loop Until Chapter 7

2 110-K2 Do While/Loop Do While condition Statements Loop The loop body is executed as long as the condition is true Flow Chart Note: if the condition is FALSE, the body of the loop is NOT executed (not even once) The statements must change the value of the condition. If not, we have a never ending loop True False Statements condition ?

3 110-K3 Example Draw three circles on a form simple solution ' Create a Pen object. Dim objPen As Pen objPen =New Pen(Color.Red, 1) ‘Draw three circles Me.CreateGraphics.DrawEllipse(objPen, 0, 0, 100, 100) Me.CreateGraphics.DrawEllipse(objPen, 0, 110, 100, 100) Me.CreateGraphics.DrawEllipse(objPen, 0, 210, 100, 100) But what if we want a column of 10 circles? Use a loop

4 110-K4 Example Dim intCount, intYCoord As Integer intCount = 1 Do While intCount <= 10 ‘Draw a circle with new Y coordinate Me.CreateGraphics.DrawEllipse(objPen, 0, intYCoord, 50, 50) ‘update coordinates intYCoordinate += 55 ‘increment counter intCount += 1 Loop

5 110-K5 What is going on? 14 intCount Beginning of the loop End of the loop 235 2534 After the loop, intCount is 6 6 Dim intCount, intYCoord As Integer intCount = 1 Do While intCount < 6 ‘Draw a circle with new Y coordinate Me.CreateGraphics.DrawEllipse(objPen, 0, intYCoord, _ 50, 50) ‘update coordinates intYCoordinate += 55 ‘increment counter intCount += 1 Loop

6 110-K6 Another example Goal: Compute the time needed to double the amount of money on a 5% interest account With a Do While loop amount<goal No goal = 2*amount Yes 5% increase year = year + 1 get the amount year = 0

7 110-K7 In VB 'Money Amount Dim decAmount As Decimal 'Investment goal Dim decGoal As Decimal 'Number of years necessary Dim intYear As Integer 'Initialization decAmount = CDec(txtInput.Text) decGoal = 2*decAmount 'Loop Do While decAmount < decGoal decAmount = decAmount + 0.05*decAmount intYear = intYear + 1 Loop 'Display the number of years lblYear.Text = FormatNumber(intYear)

8 110-K8 Other Do Loops(1) Do Statements Loop While condition The loop body is executed at least once and then as long as the condition is true. False Statements True condition ? Flow Chart

9 110-K9 Example Compare: And Dim intCount As Integer intCount = 1 Do While intCount < 1 intCount = intCount + 1 Loop lblDisplay.Text= "intCount = “& intCount Dim intCount As Integer intCount = 1 Do intCount = intCount + 1 Loop While intCount < 1 lblDisplay.Text= "intCount = “& intCount intCount = 1intCount = 2

10 110-K10 Also: Do Until / Loop and Do / Loop Until loop is executed as long as the condition is FALSE (and at least once for Do/Loop Until) Do Until condition Statements Loop The loop body is executed as long as the condition is false. Flow Chart e.g. Do Until/Loop Other Do Loops(2) False True Statements condition ?

11 110-K11 Example Problem: Starting from 1, how many consecutive integers do we need to add to get a sum greater than 200? Dim intNumber As Integer Dim intSum As Integer 'Compute 1+2+3+4+… Do Until intSum>200 intNumber = intNumber + 1 intSum = intSum + intNumber Loop ‘Display the answer lblDisplay.Text= "intNumber = “& intNumber

12 110-K12 Loop Pitfalls intCount = 1 Do Until intCount = 10 intCount = intCount + 2 Loop What happens? Get a never ending loop intCount is never 10 intCount=1 intDoubleCount = 1 Do intDoubleCount= intCount*2 intCount = intCount + 1 Loop Until intCount <= 10 What happens? VB executes the Loop only once. Don’t confuse While and Until


Download ppt "110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:"

Similar presentations


Ads by Google