Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT 5 Lesson 15 Looping.

Similar presentations


Presentation on theme: "UNIT 5 Lesson 15 Looping."— Presentation transcript:

1 UNIT 5 Lesson 15 Looping

2 Iterative Statements A procedure in a computer program that runs repeatedly until meeting certain condition is called looping. A loop can go on repetitively as long as the processor and memory could support.  For example, a program that adds a series of numbers until the sum exceeds a certain value, or a program that prompts the user to enter data repeatedly until he or she enters the word ‘Finish’. In Visual Basic 2013, there are three methods of Looping, the For…..Next loop, the Do loop and the While loop. All methods produce the same repetitive effects.

3 OVERVIEW ON LOOPS: There are three types of loops do code
Loop while Boolean expression do while Boolean expression Loop For To Step Next

4 Loops are classified two different ways:
1. When the test occur (Pre-Test or Post test) 2. By the number of times it loops (Fixed or Variable)

5 Three parts to all loops
Initialize Variable (priming the loop) ‘setting a starting value 2. Terminating condition ‘Boolean expression or variable that evaluates to false to stop the loop. 3. Increment/Decrement variable ‘Variable that is increased or decrease to make the Boolean variable False.

6 Looping using For…Next Loop
The most common looping method in VB 2013 is the For….Next loop. The structure of a For…Next loop is as shown below: For counter = startNumber to endNumber (Step increment) optional One or more Visual Basic 2013 statements Next To exit a For…..Next Loop, you can place the Exit For statement within the loop; it is normally used together with the If….Then statement. Try to avoid Exit For. Bad coding.

7 Example Dim intCounter as Integer
For intCounter = 1 to ListBox1.Items.Add (intCounter) Next * The program will enter number 1 to 10 into the list box.

8 Example Dim intCounter, intSum As Integer
For intCounter = 0 to 100 step intSum += intCounter ListBox1.Items.Add (intSum) Next * The program will calculate the sum of the numbers as follows: sum = ……

9 Example Dim intCounter, intSum As Integer intSum = 1000
For intCounter = 100 To 5 Step intSum – = intCounter ListBox1.Items.Add(intSum) Next *Notice that increment can be negative. The program will compute the subtraction as follow: 1000 – 100 – ……….

10 Example Dim intN as Integer
For intN = 1 to If intN > 6 then Exit For Else ListBox1.Items.Add (intN) End If Next The process will stop when intN is greater than 6. Remember, try to avoid using Exit For. Bad Coding

11 Looping using Do Loop The Do Loop structures are
a) Do While condition Block of one or more Visual Basic 2013 statements Loop b) Do Block of one or more Visual Basic 2013 statements Loop While condition c) Do Until condition Block of one or more Visual Basic 2012 statements Loop d) Do Block of one or more Visual Basic 2012 statements Loop Until condition * Exiting the Loop Sometime we need exit to exit a loop prematurely because a certain condition is fulfilled. The syntax to use is Exit Do. Try to avoid using Exit Do

12 Example Do while intCounter <=1000 intCounter += 1
MsgBox intCounter Loop * The above example will keep on adding until intCounter > 1000. The above example can be rewritten as Do intCounter += 1 MsgBox intCounter Loop until intCounter > 1000 intCounter += 1  intCounter = intCounter + 1

13 Example Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim intSum, intN As Integer ListBox1.Items.Add(“N” & vbTab & “Sum”) ListBox1.Items.Add(“———————-”) Do intN += intSum += intN ListBox1.Items.Add(intN & vbTab & intSum) If intN = 100 Then Exit Do End If Loop End Sub * The loop in the above example can be replaced by the following loop: Do Until intN = intN += intSum += intN ListBox1.Items.Add(intN & vbTab & intSum) Loop

14 Output

15 Looping using Do Loop The Do Loop structures are
a) Do While condition Block of one or more Visual Basic 2013 statements Loop b) Do Block of one or more Visual Basic 2013 statements Loop While condition c) Do Until condition Block of one or more Visual Basic 2012 statements Loop d) Do Block of one or more Visual Basic 2012 statements Loop Until condition

16 Do While Loop. Its syntax is the following: Do While (Boolean Expression)     (Code to execute) Loop (Expression) can be any legal logical expression that we wish to evaluate to determine whether or not to exit the loop. Each time the program reaches Loop it will verify that this expression is True, and if it is False, it will exit the loop for us. Thus, instead of exiting when an expression is True, it now exits only once this expression is false. Let's try rewriting our Fibonacci program to use a Do-While loop instead of a Do-Until loop.

17 Fibonacci numbers In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1,1,2,3,5,8,13,21,34,55,89…

18 Fibonacci program private Sub cmdClick_click() Dim intX As Integer
   Dim intY As Integer    Dim intCnt As Integer 'Our counter.    intCnt = 1    Do While intCnt < 8       msgBox intX ‘1, 1, 2, 3, 5, 8, 13, 21, ...       intX = intY + intX       intY = intX - intY       intCnt = intCnt + 1    Loop End Sub

19 Example Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim intSum, intN As Integer ListBox1.Items.Add(“n” & vbTab & “sum”) ListBox1.Items.Add(“———————-“) While intN <> 10 intN += intSum += intN ListBox1.Items.Add(intN & vbTab & intSum) End While End Sub

20 The Post-Test Loop With a post-test loop, the "loop termination decision" is tested at the bottom of the loop, therefore the statements in the body of the loop will always be executed at least one time. In VB, post-test loops are implemented with the Do/Loop statements, however, the "While" condition appear after the keyword Loop, not Do. The "While" versions of the post-test loop are flowcharted below. The only difference between the two is the placement of the "True" and "False" paths extending from the decision diamond.

21 Pre & Post Test Flowchart
Pre Test Loop

22 The Do/Loop While (Loop Until) Loop
The general format for a post-test loop in VB is: Do <list of statements> Loop While <condition> As an example of a post-test loop, the following is a code segment containing a Do/Loop construct that allows a user three tries to get his password right: Dim strPassWord As String Dim strUserTry As String Dim intNumTries As Integer strPassWord = "BEAVIS" intNumTries = 0 strUserTry = InputBox("Enter password: ") intNumTries = intNumTries + 1 Loop while strUserTry = strPassWord Or intNumTries = 3

23 Counter/Accumulator Counters:
Counter is a variable that gets increased or decreased by a constant. (Doesn’t have to be 1) Examples: intX = intX + 1 intX = intX – 1 Accumulators: Accumulators are similar to counters. An accumulator keeps a running total or sum of what is being inputted. Example: intValue = txtInput.Text intSum = intSum + intValue

24 Infinite Loops: Infinite loops are loops that never stop looping. The Boolean expression/variable never becomes false. To escape infinite loops press CTRL + BREAK


Download ppt "UNIT 5 Lesson 15 Looping."

Similar presentations


Ads by Google