1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop.

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
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.
Repeating Actions While and For 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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
An Introduction to Programming with C++ Fifth Edition
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6 - Visual Basic Schneider
5.05 Apply Looping Structures
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Chapter 5: Control Structures II (Repetition)
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 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
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 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
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 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
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
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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()
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
while Repetition Structure
Programming Logic and Design Fourth Edition, Comprehensive
Lists, Loops, Validation, and More
Repeating Program Instructions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Microsoft Visual Basic 2005: Reloaded Second Edition
Outline Altering flow of control Boolean expressions
Chapter (3) - Looping Questions.
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.
Repetition - Counting and Accumulating
Presentation transcript:

1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop

2 zDo While Loop (EXAMPLE): Dim count, sum as integer count = 1 Do while count <= 3 sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop Text1.text = sum

3 Do While Loops zStarts with Do While statement and ends with the Loop statement  Requires a condition to be checked  Conditions can contain variables, constants, properties, functions, mathmetical operators, relational operators, logical operators

4 Do While Loops  The loop executes while the condition evaluates to TRUE, the loops terminates when the condition evaluates to FALSE  PRETEST LOOP --- evaluates the condition BEFORE any code is executed

5 Do While Loops  It is possible for the loop NOT to process any code at all --- if loop is false to start  YOU MUST CONTROL THE INCREMENT OF THE CONDITIONAL EXPRESSION or you can have an endless loop

6 Do While Loops Example: zSpace Race with do while: Do while i1.top > 0 and i2.top > 0 Li_rnd1 = int((50 – ) * rnd + 10) Li_rnd2 = int((50 – ) * rnd + 10) I1.top = i1.top - li_rnd1 I2.top = i2.top - li_rnd2 Loop

7 Do Until Loop zRepeats a block of code UNTIL a condition becomes TRUE Do Code to execute Loop Until condition

8 Example: Dim count, sum as integer count = 1 Do sum = sum + val(Inputbox(“Number”, _ “Enter a Number”) count = count + 1 Loop until count > 3

9 Do Until Loop  Begins with the Do statement and ends with the Loop until Statement  Requires a condition to be checked  Conditions can contain variables, constants, properties, functions, mathmetical operators, relational operators, logical operators

10 Do Until Loop  The loop executes while the condition evaluates to FALSE, the loops terminates when the condition evaluates to TRUE  POSTTEST LOOP --- evaluates the condition AFTER any code is executed

11 Do Until Loop  This loop processes the code at least ONCE  YOU MUST CONTROL THE INCREMENT OF THE CONDITIONAL EXPRESSION or you can have an endless loop

12 Accumulators and Counters (with do while & until loops) zUsed to calculate subtotals, totals and averages zCounter --- numeric variable used for counting something (number of employees paid in a week) zaccumulator --- numeric value used to adding together something (total dollar amount of a week’s payroll)

13 Accumulators and Counters (with do while & until loops) zinitializing --- means to assign a beginning value to the counter or accumulator (start values are usually ZERO) zincrementing --- means adding to the counter or accumulator

14 Example of do while and do until using counters and accumulators: zCALCULATE THE AVERAGE SALES ENTERED BY A USER zUser clicks “do while” or “do until” CB and a series of input boxes are produced until they cancel out of the input box. The average of the sales entered is displayed on the form

15 Example of do while and do until using counters and accumulators: DO WHILE: Private Sub cmdWhile_Click() Dim strSales As String Dim intNumSales As Integer 'counter Dim sngSumSales As Single 'accumulator Dim sngAvgSales As Single strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input")

16 Example of do while and do until using counters and accumulators: Do While strSales <> "" intNumSales = intNumSales + 1 'update counter sngSumSales = sngSumSales + Val(strSales) 'update accumulator strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input") Loop

17 Example of do while and do until using counters and accumulators: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub

18 Example of do while and do until using counters and accumulators: DO UNTIL: Private Sub cmdUntil_Click() Dim strSales As String Dim intNumSales As Integer 'counter Dim sngSumSales As Single 'accumulator Dim sngAvgSales As Single strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input")

19 Example of do while and do until using counters and accumulators: DO UNTIL: Do intNumSales = intNumSales + 1 'update counter sngSumSales = sngSumSales + Val(strSales) 'update accumulator strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Input") Loop Until strSales = ""

20 Example of do while and do until using counters and accumulators: DO UNTIL: sngAvgSales = sngSumSales / intNumSales lblAvg.Caption = Format(sngAvgSales, "currency") End Sub