Lecture Set 5 Control Structures Part D - Repetition with Loops.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS0007: Introduction to Computer Programming
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic th Edition
Programming Logic and Design Eighth Edition
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
5.05 Apply Looping Structures
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 12: How Long Can This Go On?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Programming Logic and Design Fifth Edition, Comprehensive
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Tutorial 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 7 Problem Solving with 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?
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Lecture 4b Repeating With Loops
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Lecture 7: Repeating a Known Number of Times
Programming Logic and Design Fourth Edition, Comprehensive
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 5: Repetition Structures
IS 350 Loops.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
CIS 16 Application Development Programming with Visual Basic
Fundamentals of visual basic
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.
Presentation transcript:

Lecture Set 5 Control Structures Part D - Repetition with Loops

Slide 2 Objectives Write Do loops to execute statements repeatedly Write For loops to execute statements repeatedly Learn the differences between For and Do loops (and Do While and Do Until ) Lear how to use loop Exit and Continue This material should mostly review so it should go quickly – you have seen most of it already …

Slide 3 Introduction to Loops These structures are part of nearly every program written Repetition is the most powerful feature of any language – it is what computers do best (and FAST!) Learning how to construct loops with appropriate loop parameters can make difficult tasks seem easy (and coded with a short instruction sequence

Slide 4 Executing Statements Repeatedly Situations where repetition is used Calculating payroll – a loop is used to calculate the payroll for each employee Reading multiple records in a file – a loop is used to process each record in a file Graphical animations – a loop is used to move graphical objects about the screen Accounting problems – depreciation and loan balances are calculated repeatedly for multiple periods

Slide 5 Types of Loops Pre-test loops test a condition before executing the statements in a loop Post-test loops execute the loop's statements first, and then test the loop's condition

Slide 6 Do While and Do Until Loops (Syntax) Do While and Do Until loops execute statements repeatedly Do [While | Until] condition [statements] [Exit Do] [statements] Loop statements

Slide 7 Do While and Do Until Loops (Syntax, continued) Do While and Do Until loops test the condition before executing the statements in a loop The condition evaluates to a Boolean value The Do While form executes statements while the condition is True The Do Until form executes statements until the condition becomes True The Exit Do statement causes the Do loop to exit immediately statements following the loop execute

Slide 8 Execution Flow of a Do Until Loop

Slide 9 Execution Flow of a Do While Loop

Slide 10 Loops and Counters A counter is a variable whose value increases by a constant value each time through a loop The constant value used to increment the counter is typically 1 A counter takes the following general form: Counter = Counter + 1 Counter += 1

Slide 11 Loops and Counters (Example) Print the counting numbers 1 through 10 using a Do Until loop Dim Counter As Integer = 1 Do Until Counter > 10 Debug.WriteLine(Counter) Counter += 1 Loop

Slide 12 Loops and Counters (Example, continued) Print the counting numbers 1 through 10 using a Do While loop Dim Counter As Integer = 1 Do While Counter <= 10 Debug.WriteLine(Counter) Counter += 1 Loop

Slide 13 The Role of an Accumulator An accumulator is similar to a counter An accumulator is updated each time the statements in a loop execute It can be used to calculate (accumulate) a total An accumulator usually takes the following general form: Accumulator = Accumulator + value Accumulator += value But we will see other forms of “accumulators” as well – they are a powerful programming tool

Slide 14 Accumulator (Example) Store the sum of the counting numbers 1 through 10 Dim Counter As Integer = 1 Dim Accumulator As Integer = 0 Do While Counter <= 10 Debug.WriteLine(Counter) Accumulator += Counter Counter += 1 Loop

Slide 15 Infinite Loops A condition must ultimately occur causing a loop to exit Loops can be mistakenly written so that statements execute indefinitely These loops are called infinite loops Infinite loop example (Counter is always equal to 1): Dim Counter As Integer = 1 Do While Counter < 10 Debug.WriteLine(Counter) Loop

Slide 16 Nested Do Loops and Decision-making Loops can be nested, just as decision-making statements can be nested Loops can contain decision-making statements Decision-making statements can contain loops Both can contain nested sequence structures HOWEVER – it is generally a good idea NOT to nest loops inside loops or decisions. Put them in a separate procedure instead.

Slide 17 Combining Looping and Decision-making Statements (Example) Determine whether a number is prime Private Shared Function IsPrime( _ ByVal arg As Integer) As Boolean Dim Count As Integer = 2 Do While Count < arg If (arg Mod Count) = 0 Then Return False End If Count += 1 Loop Return True End Function

Slide 18 For Loops (Introduction) For loops execute statements repeatedly Use a For loop in place of a Do loop when the iteration count (the number of times the loop will execute) is known in advance For loops run more quickly than comparable Do loops For loops are more readable than equivalent Do loops

Slide 19 For Loops (Syntax) For counter = start To end [Step increment] [statements] [Exit For] [statements] Next [counter] statements

Slide 20 For Loops (Syntax, continued) The For and Next statements mark the beginning and end of a For loop counter is first initialized to start The value of counter is incremented automatically Do not explicitly set the value of counter By default, counter is incremented by one each time through the loop This value can be changed by including the Step increment clause

Slide 21 For Loops (Example) Print the counting numbers 1 to 10 Dim Counter As Integer For Counter = 1 To 10 Debug.WriteLine(Counter) Next Counter

Slide 22 Execution of a For Loop

Slide 23 Nested For Loop (Example (to be avoided) ) Print a multiplication table Dim Factor1, Factor2, Result As Integer For Factor1 = 1 To 10 For Factor2 = 1 To 10 Result = Factor1 * Factor2 Debug.Write(Result.ToString() & " ") Next Debug.WriteLine("") Next

Slide 24 Using the Step Keyword Use the Step keyword to modify a counter's value by a value other than 1 Example to decrement a counter by 5 Dim CurrentYear As Integer For CurrentYear = 2000 To 1900 Step –5 Debug.WriteLine(CurrentYear.ToString) Next CurrentYear

Slide 25 Exit and Continue Statements Can be used with any loop form Exit causes the innermost loop that contains it to exit immediately, sending control to the first instruction that follows the loop Continue causes the innermost loop that contains it to repeat immediately (effectively sending control to the bottom of the loop so that loop repetition can take place) The keywords Exit and Continue must be followed by keywords indicating the type of loop they are used in ( Exit For or Exit Do )

Slide 26 The Exit Statement

Slide 27 The Continue Statement

Slide 28 The Implications of Long-running Loops Some loops can run for hours Display messages periodically in long-running loops, or Show a progress bar as the loop is running so the user knows that progress is being made Improve the performance of long-running loops where possible Move unnecessary statements outside of a loop

Slide 29 Setting Breakpoints (A For loop with a breakpoint and an Execution Point)

Slide 30 Setting and Clearing Breakpoints