Chapter 8: More on the Repetition Structure

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
Programming Logic and Design Eighth Edition
Objectives In this chapter, you will learn about:
Chapter 2: Algorithm Discovery and Design
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 61 Post Test Loop Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
Chapter 3 Planning Your Solution
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 6: The Repetition Structure
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Chapter 7 Problem Solving with Loops
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
Chapter 9: Value-Returning Functions
More on the Selection Structure
Exam 2 Review.
REPETITION CONTROL STRUCTURE
An Introduction to Programming with C++ Sixth Edition
Exam 1 Review.
Completing the Problem-Solving Process
Chapter 3: Decisions and Loops
Topics Introduction to Repetition Structures
Chapter 10: Void Functions
Programming Logic and Design Fourth Edition, Comprehensive
CHAPTER 5A Loop Structure
The Selection Structure
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topics The if Statement The if-else Statement Comparing Strings
( Iteration / Repetition / Looping )
Topics Introduction to Repetition Structures
Topics The if Statement The if-else Statement Comparing Strings
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Objectives After studying this chapter, you should be able to:
CIS 16 Application Development Programming with Visual Basic
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
Problem Solving.
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Using C++ Arithmetic Operators and Control Structures
Programming Logic and Design Eighth Edition
Presentation transcript:

Chapter 8: More on the Repetition Structure

PA1 Re Do Day Of Week Orch Sales Main Sales Balc Sales Input Processing Output Day Of Week Orch Sales Main Sales Balc Sales Orch Costs (NC- 25) Main Floor Costs (NC - 30) Balc Costs (NC - 15) Discount Rate (NC - .1) Continue   Processing Items Total Rev, Orch Rev, Main Rev, Balc Rev Algorithm Initialize Continue, GT Orch Rev, GT Main Rev, GT Balc Rev & GT Rev Repeat while Continue = ‘Y’ Initialize Day of Week, Orch Sales, Main Sales, Balc Sales & Total Rev Input Day of Week Input Orch Sales Input Main Sales Input Balc Sales Orch Rev = Orch Sales * Orch Costs Main Rev = Main Sales * Main Floor Costs Balc Rev = Balc Sales * Balc Costs Total Rev = Orch Rev + Main Rev + Balc Rev If Day of Week = “Monday” or “Tuesday” Orch Rev = Orch Rev * (1-discount rate) Main Rev = Main Rev * (1-Main Rev) Balc Rev = Balc Rev * (1-Balc Rev) Total Rev = Total Rev * (1-discount rate) End If GT Rev = Grand Total Rev + Total Rev GT Orch Rev = GT Orch Rev + Orch Rev GT Main Rev = GT Main Rev + Main Rev GT Balc Rev = GT Balc Rev + Balc Rev Input if user would like to continue End Repeat Output GT Orch Rev | Output GT Main Rev | Output GT Balc Rev | Output GT Rev GT Orch Rev GT Main Rev GT Balc Rev GT Rev PA1 Re Do

Upcoming Important Dates 9/26 – Post Test Loops 9/29 – Value Returning Functions 10/1 – Void Functions 10/1 – PA3 Due 10/6 – Exam Review 10/8 – Exam 2: chapters 5 – 10, Testing An Introduction to Programming with C++, Seventh Edition

Objectives Include a posttest loop in pseudocode Include a posttest loop in a flowchart Code a posttest loop using the C++ do while statement Nest repetition structures Raise a number to a power using the pow function An Introduction to Programming with C++, Seventh Edition

Posttest Loops Repetition structures can be either pretest or posttest loops Pretest loop – condition evaluated before instructions are processed Posttest loop – condition evaluated after instructions are processed  Posttest loop’s instructions are always processed at least once Pretest loop’s instructions may never be processed An Introduction to Programming with C++, Seventh Edition

Flowcharting a Posttest Loop Decision symbol in a flowchart (a diamond) representing a repetition structure contains the loop condition Decision symbol appears at the top of a pretest loop, but at the bottom of a posttest loop An Introduction to Programming with C++, Seventh Edition

Flowcharting a Posttest Loop (cont’d.) Figure 8-3 Wheels & More problem specification & algorithms (continues) An Introduction to Programming with C++, Seventh Edition

Flowcharting a Posttest Loop (cont’d.) Figure 8-3 Wheels & More problem specification & algorithms (continued) An Introduction to Programming with C++, Seventh Edition

The do while Statement do while statement is used to code posttest loops in C++ Syntax: do { one or more statements to be processed one time, and thereafter as long as the condition is true } while (condition); Some programmers use a comment (such as: //begin loop) to mark beginning of loop An Introduction to Programming with C++, Seventh Edition

The do while Statement (cont’d.) Programmer must provide loop condition Must evaluate to a Boolean value May contain variables, constants, functions, arithmetic operators, comparison operators, and logical operators Programmer must also provide statements to be executed when condition evaluates to true Braces are required around statements if there are more than one An Introduction to Programming with C++, Seventh Edition

The do while Statement (cont’d.) Figure 8-9 How to use the do while statement An Introduction to Programming with C++, Seventh Edition

The do while Statement (cont’d.) Figure 8-10 IPO chart information and C++ instructions for the Wheels & More program An Introduction to Programming with C++, Seventh Edition

Nested Repetition Structures Like selection structures, repetition structures can be nested You can place one loop (the inner, or nested loop) inside another loop (the outer loop) Both loops can be pretest loops or posttest loops, or the two loops may be different types Programmer decides whether a problem requires a nested loop by analyzing the problem specification An Introduction to Programming with C++, Seventh Edition

Nested Repetition Structures (cont’d.) Figure 8-12 Logic used by a clock’s minute and second hands An Introduction to Programming with C++, Seventh Edition

The Asterisks Program Simple program that prints asterisks to the screen in different patterns First version contains a single loop Second version contains a nested loop Prints out three lines of five asterisks each using a nested for loop to print each line An Introduction to Programming with C++, Seventh Edition

The Asterisks Program (cont’d.) Figure 8-15 Problem specification IPO chart information and C++ instructions for the asterisks program An Introduction to Programming with C++, Seventh Edition

The Asterisks Program (cont’d.) Figure 8-15 IPO chart information and C++ instructions for the asterisks program (cont’d.) An Introduction to Programming with C++, Seventh Edition

The Asterisks Program (cont’d.) Figure 8-16 Completed desk-check table for the asterisks program Figure 8-17 Sample run of the asterisks program An Introduction to Programming with C++, Seventh Edition

The Asterisks Program (cont’d.) Figure 8-18 Problem specification, IPO chart information, and C++ instructions for the modified asterisks program An Introduction to Programming with C++, Seventh Edition

The Asterisks Program (cont’d.) Figure 8-18 Problem specification, IPO chart information, and C++ instructions for the modified asterisks program (cont.) An Introduction to Programming with C++, Seventh Edition

The Asterisks Program (cont’d.) Figure 8-24 Sample run of the modified asterisks program An Introduction to Programming with C++, Seventh Edition

The pow Function The pow function is a convenient tool to raise a number to a power (exponentiation) The pow function raises a number to a power and returns the result as a double number Syntax is pow(x, y), in which x is the base and y is the exponent At least one of the two arguments must be a double Program must contain the #include <cmath> directive to use the pow function An Introduction to Programming with C++, Seventh Edition

The pow Function (cont’d.) Figure 8-27 How to use the pow function An Introduction to Programming with C++, Seventh Edition

Summary A repetition structure can be either a pretest loop or a posttest loop In a pretest loop, the loop condition is evaluated before the instructions in the loop are processed In a posttest loop, the evaluation occurs after the instructions within the loop are processed Use the do while statement to code a posttest loop in C++ Use either the while statement or the for statement to code a pretest loop in C++ An Introduction to Programming with C++, Seventh Edition

Summary (cont’d.) Repetition structures can be nested, which means one loop (called the inner or nested loop) can be placed inside another loop (called the outer loop) For nested repetition structures to work correctly, the entire inner loop must be contained within the outer loop You can use the built-in C++ pow function to raise a number to a power The pow function returns the result as a double An Introduction to Programming with C++, Seventh Edition