Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Repeating Actions While and For Loops
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
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 Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Chapter 5: Control Structures II (Repetition)
An Introduction to Programming with C++ Fifth Edition
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 6 - Visual Basic Schneider
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 4: Control Structures II
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Nested LOOPS.
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.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
Computer Programming -1-
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 5: Repetition Structures
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.
Repetition and Loop Statements
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Let’s all Repeat Together
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Explore the iteration structure Implement iteration using the Do While and Do Until statements Implement iteration using the For Next statement Create nested structures

Visual Basic.NET Programming: From Problem Analysis to Program Design3 Exploring the Iteration Structure Iteration structure –Execute one or more statements repeatedly –Often called a loop –Write a set of statements Repeatedly execute them until terminating condition is reached

Visual Basic.NET Programming: From Problem Analysis to Program Design4 Iteration Logic Basic iteration logic: –First test to see whether loop should terminate –If loop does not terminate Statement or statements in loop body executed –Control returns to beginning

Visual Basic.NET Programming: From Problem Analysis to Program Design5

6 Iteration Logic (continued) Infinite loop –Never ends –Terminating condition never occurs Pre-test loop –Terminating condition checked before body of loop executed –Statements in loop may not be executed If terminating condition met

Visual Basic.NET Programming: From Problem Analysis to Program Design7 Iteration Logic (continued) Post-test loop –Terminating condition checked after body of loop executed –Statements in loop executed at least once Regardless of terminating condition

Visual Basic.NET Programming: From Problem Analysis to Program Design8

9 Controlling Iteration Counter control –Employs Integer variable to count number of times loop has executed –Variable called counter –Update counter each time loop executes

Visual Basic.NET Programming: From Problem Analysis to Program Design10 Controlling Iteration (continued) Counter control (continued) –When counter reaches predetermined value Loop terminates –Can increment or decrement counter by any value that suits program logic

Visual Basic.NET Programming: From Problem Analysis to Program Design11

Visual Basic.NET Programming: From Problem Analysis to Program Design12 Controlling Iteration (continued) Sentinel-control logic –Checks user input for specific value –Terminates when value detected –Value called sentinel Should be unique value

Visual Basic.NET Programming: From Problem Analysis to Program Design13

Visual Basic.NET Programming: From Problem Analysis to Program Design14 Implementing Iteration Using the Do While and Do Until Statements Iteration statements: –Do Do While Do Until –For

Visual Basic.NET Programming: From Problem Analysis to Program Design15 Writing Loops Using Do While Pre-test loop –Terminating condition is tested at beginning of loop Syntax: Do While expression Statements Loop Loop continues to execute as long as expression is true

Visual Basic.NET Programming: From Problem Analysis to Program Design16 Example 5-3: Computing an exam average using a counter- controlled Do While loop 1. ' define variables 2. Dim sum, average AsDouble 3. Dim numberOfExams As Integer = 5 4. Dim counter As Integer = 1 …

Visual Basic.NET Programming: From Problem Analysis to Program Design17 Example 5-3: Computing an exam average using a counter- controlled Do While loop … 5. ' begin loop 6. Do While (counter <= numberOfExams) 7. Console.WriteLine(“Enter an Exam Score: “) 8. sum = sum + Convert.ToDouble(Console.ReadLine()) 9. counter += 1 ' count the number of iterations 10. Loop …

Visual Basic.NET Programming: From Problem Analysis to Program Design18 Example 5-3: Computing an exam average using a counter- controlled Do While loop … 11. ' compute & display the average 12. average = sum / numberOfExams 13. Console.WriteLine(“The average is: “ & Math.Round(average, 1)) …

Visual Basic.NET Programming: From Problem Analysis to Program Design19

Visual Basic.NET Programming: From Problem Analysis to Program Design20 Writing Loops Using Do Until Executes until expression is true –Contrast to Do While Syntax Do Until expression Statement(s) Loop

Visual Basic.NET Programming: From Problem Analysis to Program Design21

Visual Basic.NET Programming: From Problem Analysis to Program Design22 Implementing Iteration Using the For Next Statement For Next loops provide only: –Counter-controlled loop –Pre-test loop Initializes counter variable Automatically increments counter –Simplifies and shortens code

Visual Basic.NET Programming: From Problem Analysis to Program Design23

Visual Basic.NET Programming: From Problem Analysis to Program Design24

Visual Basic.NET Programming: From Problem Analysis to Program Design25 Example 5-9: Computing an Exam Average Using a For Next Loop 1. ' define variables 2. Dim sum, average As Double 3. Dim numberOfExams As Integer = 5 4. Dim counter As Integer 5. ' begin loop 6. For counter = 1 To 5 Step 1 7. Console.WriteLine("Enter an Exam Score: ") …

Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 5-9: Computing an Exam Average Using a For Next Loop (continued) 8. sum = sum + Convert.ToDouble(Console.ReadLine()) 9. Next 10. ' compute & display the average 11. average = sum / numberOfExams 12. Console.WriteLine("The average is: " & Math.Round(average, 1)) Look at ex and 5-11 to see the difference between For Next and Do while

Visual Basic.NET Programming: From Problem Analysis to Program Design27 Creating Nested Structures Nested loop –One iteration structure placed inside another Can place iteration inside selection –And vice versa

Visual Basic.NET Programming: From Problem Analysis to Program Design28 Example 5-12: Computing an Exam Average Using a Nested For Next Loop (excerpt) For studentCounter = 1 To numberOfStudents 8. sum = 0 ' reinitialize sum 9. ' begin inner loop for exams 10. For examCounter = 1 To numberOfExams 11. Console.WriteLine (“Enter an Exam Score: “) 12. score = Convert.ToDouble (Console.ReadLine())

Visual Basic.NET Programming: From Problem Analysis to Program Design29 Example 5-12: Computing an Exam Average Using a Nested For Next Loop (continued) 13. sum += score 14. Next ' end of inner loop 15. average = sum / numberOfExams 16. Console.WriteLine (“The average is: “ & Math.Round(average, 1)) 17. Next ' end of outer loop

Visual Basic.NET Programming: From Problem Analysis to Program Design30

Visual Basic.NET Programming: From Problem Analysis to Program Design31 Summary Use iteration structure to execute one or more statements repeatedly –Also called a loop Loop logic types: –Pre-test –Post-test Control number of times a loop executes using: –Counter control –Sentinel control

Visual Basic.NET Programming: From Problem Analysis to Program Design32 Summary (continued) Visual Basic.NET Loop structures: –Do While –Do Until –For Iteration and selection structures can be nested For in-class practice: Modify Ex to calculate average grade for the whole class; Review ex. 5-13: PEx. 3 (without re-prompt), 4 (without re-prompt) Homework: p. 172, PEx. 6; Due: 2/29/08