Component 4/Unit 5-4. Iteration (Repetition, Looping, Do loops) Loops are used to execute statements repetitively Loops require a conditional test that.

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.
Programming Logic and Design Seventh Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design Seventh Edition
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Recursion CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Chapter 2 The Algorithmic Foundations of Computer Science
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chapter 6 - Visual Basic Schneider
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chapter 2: Algorithm Discovery and Design
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
PRE-PROGRAMMING PHASE
5.05 Apply Looping Structures
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Simple Program Design Third Edition A Step-by-Step Approach
Invitation to Computer Science, Java Version, Second Edition.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Component 4/Unit 5-6. Instantiation With the class/design in hand, an actual automobile can be made on an assembly line (instantiated), or a patient can.
Problem Solving using the Science of Computing MSE 2400 EaLiCaRA Spring 2015 Dr. Tom Way.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Cs413_design04.ppt Design and Software Development Design : to create a functional interface that has high usability Development : an organized approach.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Component 4/Unit 5-5. Topic 5 Additional Programming Concepts Modularity and strong cohesive code Conditional and Unconditional Branching Classes Instantiation.
Component 4/Unit 5-3. Data Type Alphanumeric (Character set: A-Z, 0-9 and some special characters) –Customer address, name, phone number, Customer ID,
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
Controlling Program Flow with Decision 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. ◦
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Programming Techniques
Computer Science Up Down Controls, Decisions and Random Numbers.
CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.
Processing multiple files
Lesson #5 Repetition and Loops.
Lesson #5 Repetition and Loops.
Think What will be the output?
Chapter 5: Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Lesson #5 Repetition and Loops.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Algorithm Discovery and Design
Three Special Structures – Case, Do While, and Do Until
The structure of programming
Lesson #5 Repetition and Loops.
Repetition - Counting and Accumulating
Presentation transcript:

Component 4/Unit 5-4

Iteration (Repetition, Looping, Do loops) Loops are used to execute statements repetitively Loops require a conditional test that is supposed to end the loop Endless loops (logic error) Pre-test loops Post-test loops Component 4/Unit 52 Health IT Workforce Curriculum Version 1.0/Fall 2010

Problem Statement Design and write a program that processes a file of debit and credit transactions and produces a report showing the transaction type (DB or CR), the transaction amount, a running count of debits, a running debit total, a running count of credits and a running credit total. Health IT Workforce Curriculum Version 1.0/Fall Component 4/Unit 5

Pseudocode Solution Involving Alternation and Iteration SumDBandCR module Open File Output Heading Do pre-test Until EOF Input TranType, TranAmt If TranType = "DB" DebitAmt = TranAmt DBTotal = DBTotal + DebitAmt CountOfDBs = CountOfDBs + 1 ElseIf TranType = "CR" CreditAmt = TranAmt CRTotal = CRTotal + CreditAmt CountOfCRs = CountOfCRs + 1 End If Output TranType, TranAmt, CountOfDBs, DBTotal, CountOfCRs, CRTotal End pre-test Close File End module Health IT Workforce Curriculum Version 1.0/Fall Component 4/Unit 5

Example Code Showing Alternation and Iteration 1 Dim TranType As String 2 Dim TranAmt As Currency 3 Dim DebitAmt As Currency 4 Dim CreditAmt As Currency 5 Dim DBTotal As Currency 6 Dim CRTotal As Currency 7 Dim CountOfDBs As Integer 8 Dim CountOfCRs As Integer 9 Option Explicit 10 Private Sub cmdSumDBandCR_Click() 11 Open ActiveDocument.Path & "/TranFile.txt" For Input As #1 12 lblReport.Caption = "Tran type Tran amount DB count DB total CR count CR total“ 13 ‘Repetitive execution (14, 27) 14 Do Until EOF(1) 15 Input #1, TranType, TranAmt 16‘Exclusive options (17, 21, 25) 17 If TranType = "DB" Then 18 DebitAmt = TranAmt 19 DBTotal = DBTotal + DebitAmt 20 CountOfDBs = CountOfDBs ElseIf TranType = "CR" Then 22 CreditAmt = TranAmt 23 CRTotal = CRTotal + CreditAmt 24 CountOfCRs = CountOfCRs End If 26 lblReport.Caption = lblReport.Caption & vbNewLine & _ " " & TranType & " " & _ TranAmt & " " & _ CountOfDBs & " " & _ DBTotal & " " & _ CountOfCRs & " " & _ CRTotal 27 Loop 28 Close #1 29 End Sub

Concurrency Concurrent programming processes (sometimes called Parallel processes) are subtasks of a larger task that are carried out independent of one another and the results are recompiled later to form a single solution. This can be done in a single processor or through a network of multiple machines/processors in what is called distributed processing. Health IT Workforce Curriculum Version 1.0/Fall Component 4/Unit 5

Concurrency Single processor concurrency –Standing in line to buy tickets to a show and asking the person behind you to hold your place while you stand in line to buy a hotdog down the street. –Driving and talking on the phone at the same time Multiple processor concurrency –Baseball team playing defense –Airplane pilot and copilot flying a plane

Recursion Recursion is difficult to define. There are many jokes about recursive definitions. One is that to find the definition of recursion you need to see Recursion. Recursive processing in programming is when a process invokes itself. Health IT Workforce Curriculum Version 1.0/Fall Component 4/Unit 5

Recursion Definition Continued In programming a process is said to be recursive if the following two things are true. 1.A base statement is created 2.A set of statements that reduce all cases to the base statement. Health IT Workforce Curriculum Version 1.0/Fall Component 4/Unit 5

Recursion Definition Continued Given that you know how to process a single drug prescription, to process 1000 prescriptions do the following. 1. Process the first prescription (Base statement) and remember how that was done. 2. Process the first prescription of what is left (999). This means that the base statement is executed again (process has called upon itself) Health IT Workforce Curriculum Version 1.0/Fall Component 4/Unit 5

Recursion Definition Continued “If at first you don’t succeed, try, try again”. Opposite mirrors generate a recursive image. Spell checker that checks the first word and then requires the rest of the document to be submitted for spell checking