UNIT 5 Lesson 15 Looping.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
CS0004: Introduction to Programming Repetition – Do Loops.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
5.05 Apply Looping Structures
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
ENGR 112 Decision Structures.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Computer Science Up Down Controls, Decisions and Random Numbers.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5- Control Structures: Part 2
Loop Structures.
Lists, Loops, Validation, and More
Control Structures: Part 2
3rd prep. – 2nd Term MOE Book Questions.
JavaScript: Control Statements.
Lecture 07 More Repetition Richard Gesick.
Chapter 5 – Control Structures: Part 2
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Lecture 4B More Repetition Richard Gesick
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Looping and Repetition
Outline Altering flow of control Boolean expressions
Repetition Structures
Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
Case & Repetitive Statements
Prepared By: Deborah Becker
Conditional Loops Counted Loops
Using C++ Arithmetic Operators and Control Structures
Review of Previous Lesson
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

UNIT 5 Lesson 15 Looping

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.

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

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)

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.

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.

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

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

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

Example Dim intN as Integer For intN = 1 to 10 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

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

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

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 += 1 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 = 10 intN += 1 intSum += intN ListBox1.Items.Add(intN & vbTab & intSum) Loop

Output

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

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.

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…

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

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 += 1 intSum += intN ListBox1.Items.Add(intN & vbTab & intSum) End While End Sub

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.

Pre & Post Test Flowchart Pre Test Loop

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

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

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