130 likes | 314 Views
Iterations (1). Do While / Loop , Do / Loop While Do Until / Loop , Do / Loop Until. Chapter 7. To repeat a piece of code :. Up to now , need to call the procedure again (e.g. click again on a command button). Can also use loops:. Do Loops. Do While/Loop. The loop body is executed
E N D
Iterations (1) DoWhile/Loop, Do/LoopWhile Do Until/Loop, Do/LoopUntil Chapter 7 To repeat a piece of code: Up to now, need to call the procedure again (e.g. click again on a command button) Can also use loops: Do Loops
Do While/Loop The loop body is executed as long as the condition is true The statements must change the value of the condition. If not, we have a never ending loop True condition ? Statements False Note: if the condition is FALSE, the body of the loop is NOT executed (not even once) Do Whilecondition Statements Loop Flow Chart
Example Draw three circles on a form simple solution Use a loop ' 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?
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
What is going on? intCount Beginning of the loop End of the loop After the loop, intCount is 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 1 2 3 4 5 6 2 3 4 5
Another example With a Do While loop get the amount goal = 2*amount year = 0 5% increase year = year + 1 Yes No Goal: Compute the time needed to double the amount of money on a 5% interest account amount<goal
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)
Other Do Loops(1) The loop body is executed at least once and then as long as the condition is true. Statements True condition ? False Do Statements Loop Whilecondition Flow Chart
Example Compare: intCount = 1 intCount = 2 Dim intCount As Integer intCount = 1 Do While intCount < 1 intCount = intCount + 1 Loop lblDisplay.Text= "intCount = “& intCount And Dim intCount As Integer intCount = 1 Do intCount = intCount + 1 Loop While intCount < 1 lblDisplay.Text= "intCount = “& intCount
Other Do Loops(2) loop is executed as long as the condition isFALSE (and at least once forDo/Loop Until) The loop body is executed as long as the condition is false. False condition ? Statements True Also:Do Until / Loop and Do / Loop Until e.g.Do Until/Loop Do Untilcondition Statements Loop Flow Chart
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
Loop Pitfalls Get a never ending loop intCount is never 10 VB executes the Loop only once. Don’t confuse While and Until intCount=1 Do Until intCount= 10 intCount = intCount + 2 Loop What happens? intCount=1 intDoubleCount = 1 Do intDoubleCount= intCount*2 intCount = intCount + 1 Loop Until intCount<=10 What happens?