© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

4 Control Statements: Part 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6: Modular Programming Problem Solving & Program Design in.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Week 2 The Object-Oriented Approach to Requirements
EU market situation for eggs and poultry Management Committee 20 October 2011.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Adding Up In Chunks.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Note to the teacher: Was 28. A. to B. you C. said D. on Note to the teacher: Make this slide correct answer be C and sound to be “said”. to said you on.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS 240 Computer Programming 1
Chapter 10: The Traditional Approach to Design
Analyzing Genes and Genomes
Systems Analysis and Design in a Changing World, Fifth Edition
Types of selection structures
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Essential Cell Biology
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 15 2 k Factorial Experiments and Fractions.
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
© 2012Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
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 5: Repetition and Loop Statements By: Suraya Alias.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 5 Repetition and Loop Statements. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.5-2 Figure 5.1 Flow Diagram of Loop Choice Process.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Chapter 5: Repetition Structures
Chapter 5: Repetition and Loop Statements
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Presentation transcript:

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

1-2 © 2010 Pearson Addison-Wesley. All rights reserved. 5-2 Introduction to Repetition Structures A repetition structure causes a statement or set of statements to execute repeatedly Allow a programmer to avoid duplicate code –Duplicate code makes a program large –Write a long sequence of statements is time consuming –If part of the duplicate code has to be corrected or changed, then the change has to be done many times

1-3 © 2010 Pearson Addison-Wesley. All rights reserved. 5-3 Condition-Controlled Loops While Loop –While a condition is true, do some task Do-While Loop –Do some task, while condition is true Do-Until Loop –Do some task, while a condition is false (or until its true) With all loops, be careful not to create infinite loops – always provide a way to break out

1-4 © 2010 Pearson Addison-Wesley. All rights reserved. 5-4 Condition-Controlled Loops The While Loop – pretest loop While condition Statement End While Figure 5-1 The logic of a While loop

1-5 © 2010 Pearson Addison-Wesley. All rights reserved. 5-5 Condition-Controlled Loops Working with Modules and Loops To run a program multiple times, modules can be put within a loop Figure 5-5 The main module of Program 5-3

1-6 © 2010 Pearson Addison-Wesley. All rights reserved. 5-6 Condition-Controlled Loops The Do-While Loop – posttest loop Do Statement While condition Figure 5-8 Flowchart for the main module in Program 5-4

1-7 © 2010 Pearson Addison-Wesley. All rights reserved. 5-7 Condition-Controlled Loops The Do-Until Loop Loop iterates until a condition is true, but not all languages support this type of loop Figure 5-10 The logic of a Do-Until loop

1-8 © 2010 Pearson Addison-Wesley. All rights reserved. 5-8 Count-Controlled Loops A count-controlled loop iterates a specific number of times A for loop is best used for this situation For counterVariable = startingValue to maxValue statement End for There is an Initialization, Test, and Increment expression that controls the loop

1-9 © 2010 Pearson Addison-Wesley. All rights reserved. 5-9 Count-Controlled Loops For loops can also increment by more than one, count backwards by decrementing, or allow the user to control the number of interactions The for loop in action Figure 5-14 Flowchart for Program 5-8

1-10 © 2010 Pearson Addison-Wesley. All rights reserved Count-Controlled Loops General loop concerns Do not forget to initialize the loop control variable Do not forget to modify the loop control variable Many loops are interchangeable, but generally –Use while loop when loop may not have to process –Use do while when it must process at least once –Use for loop with specific number of iterations

1-11 © 2010 Pearson Addison-Wesley. All rights reserved Calculating a Running Total A running total is a sum of number that accumul ates with each iteration of a loop

1-12 © 2010 Pearson Addison-Wesley. All rights reserved Sentinels A sentinel is a special value that marks the end of a list of values, used as stop values for loops How it can be done –Ask the user at the end of each loop iteration, if there is another value to process –Ask the user at the beginning of the loop, how many times the loop should process

1-13 © 2010 Pearson Addison-Wesley. All rights reserved Sentinels – Controlled loop 1.Initialize sum to zero. 2.Get first Score 3.While score is not the sentinel a.Add score to sum b.Get the next score #define SENTINEL -99 printf(Enter first score (or %d to quit),SENTINEL); scanf(%d, &score); while (score != SENTINEL){ sum += score; printf(Enter next score (%d to quit), SENTINEL); scanf(%d, &score); } printf(\nSum of exam scores is %d\n, sum); printf(Enter first score (or %d to quit),SENTINEL); for ( scanf(%d, &score); score != SENTINEL; scanf(%d, &score)) { sum += score; printf(Enter next score (%d to quit), SENTINEL); }

1-14 © 2010 Pearson Addison-Wesley. All rights reserved Nested Loops All loops can be nested, that is, a loop inside of a loop Figure 5-21 Flowchart for a clock simulator

1-15 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.1 Flow Diagram of Loop Choice Process

1-16 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.2 Program Fragment with a Loop

1-17 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.3 Flowchart for a while Loop

1-18 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.4 Program to Compute Company Payroll

1-19 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.4 Program to Compute Company Payroll

1-20 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.5 Using a for Statement in a Counting Loop

1-21 © 2010 Pearson Addison-Wesley. All rights reserved *Multiplication Table of 9*/ /* Displays the 9s Table */ #include /* printf, scanf definitions */ #define MULT_NUM 9 int main(void) { int num, answer = 0; /* Display 9s Table */ printf("The 9's Multiplication Table \n\n"); for (num = 0; num < 10; ++num) { answer = num * MULT_NUM; printf("%d times %d = %d \n", num, MULT_NUM, answer); } printf(" \n"); return (0); }

1-22 © 2010 Pearson Addison-Wesley. All rights reserved /*Multiplication Tables*/ /* Displays all the tables */ #include /* printf, scanf definitions */ int main(void) { int num, mult_num, answer = 0; /* Display 9s Table */ printf("The Multiplication Tables \n\n"); for (num = 0; num < 10; ++num) { for(mult_num = 0; mult_num < 10; ++mult_num){ answer = num * mult_num; printf("%d times %d = %d \n", num, mult_num, answer); } printf(" \n"); } printf(" \n"); return (0); }

1-23 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.6 Comparison of Prefix and Postfix Increments

1-24 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.7 Function to Compute Factorial

1-25 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.8 Displaying a Celsius-to-Fahrenheit Conversion Table

1-26 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.9 Program to Monitor Gasoline Storage Tank

1-27 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.9 Program to Monitor Gasoline Storage Tank

1-28 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.9 Program to Monitor Gasoline Storage Tank

1-29 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.10 Sentinel-Controlled while Loop

1-30 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.11 Batch Version of Sum of Exam Scores Program

1-31 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.12 Program to Process Bald Eagle Sightings for a Year

1-32 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.12 Program to Process Bald Eagle Sightings for a Year

1-33 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.13 Nested Counting Loop Program

1-34 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.14 Validating Input Using do-while Statement

1-35 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.15 Structure Chart for Computing Solar Collecting Area Size

1-36 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.16 Program to Approximate Solar Collecting Area Size

1-37 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.16 Program to Approximate Solar Collecting Area Size

1-38 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.16 Program to Approximate Solar Collecting Area Size

1-39 © 2010 Pearson Addison-Wesley. All rights reserved Figure 5.16 Program to Approximate Solar Collecting Area Size