Prepared By: Deborah Becker

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Adding Automated Functionality to Office Applications.
5.05 Apply Looping Structures
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
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:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 12: How Long Can This Go On?
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.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Introduction to Problem Solving and Control Statements.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Chapter 6 - VB 2005 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:
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Controlling Program Flow with Looping Structures
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CSET 3250 Client-Side Scripting VBScript : Operators, Control Structures, Procedures and Functions.
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.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
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.
1 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: Analyze a Loan.
UNIT 5 Lesson 15 Looping.
Chapter 4 Repetition Statements (loops)
Repetition Structures Chapter 9
Visual Basic 6 (VB6) Data Types, And Operators
The switch Statement, and Introduction to Looping
Problem Solving and Control Statements: Part 2
Computer Programming I
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Lists, Loops, Validation, and More
JavaScript: Control Statements.
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.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structures: Part 1
Outline Altering flow of control Boolean expressions
CIS 16 Application Development Programming with Visual Basic
Module 4 Loops.
Do … Loop Until (condition is true)
Introduction to Problem Solving and Control Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
Case & Repetitive Statements
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
ASP control structure BRANCHING STATEMENTS
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Looping and Repetition
Presentation transcript:

Prepared By: Deborah Becker This presentation will probably involve audience discussion, which will create action items. Use PowerPoint to keep track of these action items during your presentation In Slide Show, click on the right mouse button Select “Meeting Minder” Select the “Action Items” tab Type in action items as they come up Click OK to dismiss this box This will automatically create an Action Item slide at the end of your presentation with your points entered. Looping 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Topics of Discussion Do Loops For Next Nesting Looping Structures Visual Basic like other programming languages executes lines of code from the top of the program downwards. However, often a programmer does not wish all lines to be executed every time the program is run. Instead of writing several different programs for this job, branching and repeating commands can be used. Repeating Structures (or Loops) Branching commands are used to determine when to execute a small section of code and when not to run it. One example is the ‘If’ command. If something is true then the code will be run, if not it will not be executed and VB will continue further down the program. Repeating commands are useful for running small sections of code several times. If a programmer needs to read 200 lines of text from a file he could use 200 lines of code, one for every read statement, or alternatively a ‘For Next’ loop could do a similar job using just three or four lines! 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Looping Structures Do...Loop Until Do...Loop While Do While...Loop Do Until...Loop For…Next While…Wend The easiest way to understand repetition is with the aid of a practical example. Consider the steps a Chip Delivery Driver must perform as he delivers his chips to all the stores on his route. There are four basic steps: 1) check number of bags of chips requested by the store, 2) fetch required bags of chips from his truck, 3) carry back stales from store, and 4) travel to next store. If the route has 20 stores then the salesman has to carry out 80 individual steps to deliver all the chips. A computer simulation of this could be written in Visual Basic using 80 lines of code, one line for each step. However, this is very inefficient. A much better way is to ‘wrap’ the four basic steps in some kind of repeating structure. Visual Basic supports the following types: 4/25/2019 Prepared By: Deborah Becker

Information Flow in a Do Loop l l l l l l l Do statement1 statement2 ... Loop 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Action of Do...Loop Until Condition False Condition True Do statement1 statement2 . . . Do statement1 statement2 . . . Loop Until condition Loop Until condition statement 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker The 'Do ... Loop Until Dim intStoreNumber as Integer ‘Initialize variable intStoreNumber = 1 Do Count Number of Chips delivered Count Number of Stales Removed ‘Increment variable intStoreNumber intStoreNumber = intStoreNumber + 1 Loop until intStoreNumber > 20 This type of repeating structure will always be executed at least once because the loop exit statement is at the end. The Chip Delivery problem could be solved in the following way: intStoreNumber = 1 Do ' Check the order ' Fetch correct order ' Carry back stales intStoreNumber = intStoreNumber + 1 Loop Until intStoreNumber > 20 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker ?’s When is the condition checked? How many times will the….. Do Loop Until execute? 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Action of Do...Loop While Condition True Condition False Do statement1 statement2 . . . Do statement1 statement2 . . . Loop While condition statement Loop While condition 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker ?’s When is the condition checked? How many times will the….. Do While Loop execute? 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker So the Do…Loop While…. is very similar to the previous ‘Do...Loop Until’. The difference is in the logic of the exit condition. For example, the ‘Loop While’ will repeat a block of instructions while a condition remains true, but the ‘Loop Until’ loop will exit when the exit condition turns true. A ‘Do...Loop While’ loop is very similar to the previous ‘Do...Loop Until’. The difference is in the logic of the exit condition. For example, the ‘Loop While’ will repeat a block of instructions while a condition remains true, but the ‘Loop Until’ loop will exit when the exit condition turns true. 4/25/2019 Prepared By: Deborah Becker

Action of Do While…Loop Condition False Condition True Do While condition statement1 statement2 . . . Do While condition statement1 statement2 . . . Loop statement Loop 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Do While...Loop Do While...Loop (Will this loop ever execute?): Dim a as Integer a = 1 Do While a > 10 a = a - 1 Loop This loop is functionally equivalent to the ‘Do...Loop While’ except that the loop test condition is tested at the start of the loop. In practice this means that the ‘Do While...Loop’ might not be executed at all (if condition is false), whereas the ‘Do...Loop While’ will always be executed at least once. 4/25/2019 Prepared By: Deborah Becker

How Many Times Will This Do While…Loop Execute Dim a as Byte a = 20 Do While a > 10 a = a - 1 Loop 20-1 = 19 19-1 = 18 18-1 =17 17-1 = 16 16-1 = 15 15-1 = 14 14-1 = 13 13-1 = 12 12-1 = 11 11-1 = 10 10-1 = 9 9-1 = 8 8-1 = 7 7-1 = 6 6-1 = 5 5-1 = 4 4-1 = 3 3-1 = 2 2-1 = 1 1-1 = 0 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 = 10 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Action of Do Until . . . Loop Condition False Condition True Do Until condition statement1 statement2 . . . Do Until condition statement1 statement2 . . . Loop Loop statement 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Do Until…Loop intStoreNumber = 1 Do Until intStoreNumber > 20 ‘Check the Order ‘Fetch the Order ‘Carry Back the Stales intStoreNumber = intStoreNumber + 1 Loop This loop, like the previous ‘Do While…Loop’ type, tests the condition at the beginning of the loop. However, whereas the previous loop repeats while the condition is true, the ‘Do Until…Loop’ repeats until the condition is true. 4/25/2019 Prepared By: Deborah Becker

Equivalent Do While and Do Until loops Private Sub DoWhileTest_Click() Dim X As Integer Do While X < 3 MsgBox X X = X + 1 Loop End Sub Private Sub DoUntilTest_Click() Dim X As Integer Do Until X > = 3 MsgBox X X = X + 1 Loop End Sub 0, 1, 2 0, 1, 2 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker The 'While ... Wend' CONTROL Executes a series of statements as long as the condition is True. Private Sub Form_Activate() Dim strPassword As String Const strSysPassW = help2003 While strPassword <> strSysPassW strPassword = InputBox("Please enter _ your Password:", “Password", "") Wend End Sub The ‘While...Wend’ loop is functionally equivalent to the ‘Do While...Loop’ discussed above. Once again this type of repeating structure should be used in situations where the loop will execute at least once. For example, a program which checks ban PIN numbers would always execute at least once: Note: In reality an InputBox would not be used since the typed PIN can be seen on the screen and also the correct PIN would not be held in a constant but instead retrieved from an encrypted file, but as an example it demonstrates the potential of ‘While...Wend’ loops. 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker The Endless Program An infinite loop is a loop created where the condition will never be reached. 1.) Closing the Window or 2.) Pressing Ctrl-Break. *Make sure that the terminating condition can be reached………….. 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Terminating the Loops Do...Loop Until—repeats until the condition turns true Do...Loop While—repeats while condition remains true Do While...Loop—repeats while condition is true Do Until...Loop—repeats until the condition is true For...Next –used when the # of iterations is known Terminates when the upper limit of the loop is reached While...Wend—equivalent to Do While Should be used in situations where the loop must execute at least once 4/25/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Nesting loops Dim intCupsFilled , I as Integer intCupsFilled = 0 Do ‘Post condition loop ' boil water ' add tea bags to pot ' fill pot with boiling water For i = 1 to 5 ' pour tea into cup ' move to next cup intCupsFilled += 1 Next ‘(i is increased here) Loop Until intCupsFilled = 100 In the Delivery Man example above the delivery man performs the basic 4 steps at each of the 20 stores he serves. However, consider the more complex problem of simulating the pouring of cups of tea at a Church meeting. The problem is more complex because the tea pot holds only 5 cups of tea and there are 100 to serve. To solve this problem two loops can be used, one inside another (nested): Note: Different types of loops can be nested inside one another. In the above example the ‘For...Next’ loop is nested within a ‘Do...Loop Until’ structure. 4/25/2019 Prepared By: Deborah Becker