Presentation © Copyright 2002, Bryan Meyers Top-Down, Structured Program Design Chapter 5.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Repetition Control Structures
Repetition control structures
ITEC113 Algorithms and Programming Techniques
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Chapter 5: Control Structures II (Repetition)
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Repetition Control Structures
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
The switch Statement, DecimalFormat, and Introduction to Looping
Getting Started Chapter 2 Presentation © Copyright 2002, Bryan Meyers
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CPS120 Introduction to Computer Science Iteration (Looping)
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
ITEC113 Algorithms and Programming Techniques
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Welcome to RPG544. Bit about Cindy Administrative Stuff Standards Due Dates Web Page.
5-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
CiS 260: App Dev I Chapter 4: Control Structures II.
Computer Science Faculty
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 8 JavaScript: Control Statements, Part 2
Iteration: Beyond the Basic PERFORM
2.6 The if/else Selection Structure
Chapter 4 - Program Control
IBC233 Lecture 3 Updated Fall 2011
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Presentation © Copyright 2002, Bryan Meyers Top-Down, Structured Program Design Chapter 5

Programming in RPG IV Third Edition 2 Objectives Determine how to design and write structured RPG programs Use structured RPG operations that perform sequence, selection, and iteration Use subroutines Design a control break program

Programming in RPG IV Third Edition 3 Structured Design A methodology that limits its control to 3 basic structures –Sequence –Selection (decision) –Iteration (repetition, looping) Each of these control structures has a single entry point and a single exit point

Programming in RPG IV Third Edition 4 Sequence Control Structure Statement

Programming in RPG IV Third Edition 5 Selection Control Structure Condition? Statement(s) TrueFalse

Programming in RPG IV Third Edition 6 Iteration Control Structure Condition? Statement(s) True False

Programming in RPG IV Third Edition 7 Relational Codes SymbolCodeMeaning >GTGreater than <LTLess than =EQEqual to <>NENot equal to <=LELess than or equal to >=GEGreater than or equal to

Programming in RPG IV Third Edition 8 Collating sequence Character by character comparison EBCDIC Digits are compared based on algebraic value Letters are smaller than digits Lowercase letters are smaller then uppercase letters A blank is smaller than any other displayable character

Programming in RPG IV Third Edition 9 Selection Operations IF Conditional expression –ELSE –ENDIF AND/OR Nested IFs –ELSEIF SELECT –WHEN –ENDSL

Programming in RPG IV Third Edition 10 IF / ENDIF If the comparison is true all statements between the IF and ENDIF are performed If the comparison is false the statements are bypassed * /FREE IF Age >= 65; SeniorCnt = SeniorCnt + 1; ENDIF; /END-FREE

Programming in RPG IV Third Edition 11 IF / ELSE / ENDIF If the comparison is true all statements between the IF and ELSE are performed If the comparison is false all the statements between the ELSE and the ENDIF are performed * /FREE IF Hours <= 40; EVAL(H) TotalPay = Hours * PayRate; ELSE; EVAL(H) TotalPay = 40 * PayRate + (Hours - 40) * PayRate * 1.5; ENDIF; /END-FREE

Programming in RPG IV Third Edition 12 AND / OR AND: Both relationships must be true OR: One or the other or both relationships must be true * /FREE IF Age >= 65 AND Status = 'R'; // This block of code is executed only if Age >=65 and Status =R. ELSE; // If one or both conditions are not met,this code is executed. ENDIF; IF Age >= 65 OR Status = 'R'; // This block is executed if one or both conditions are true. ELSE; // This block is executed only if both conditions are false. ENDIF; /END-FREE

Programming in RPG IV Third Edition 13 Nested IFs Build IFs within IFs –Each IF requires an ENDIF * /FREE IF Sales <=5000; Rate =.005; ELSE; IF Sales <=10000; Rate =.0075; ELSE; IF Sales <=20000; Rate =.01; ELSE; Rate =.015; ENDIF; /END-FREE

Programming in RPG IV Third Edition 14 Nested IFs ELSEIF combines function of ELSE and IF statement –Single ENDIF ends IF/ELSEIF blocks * /FREE IF Sales <=5000; Rate =.005; ELSEIF Sales <=10000; Rate =.0075; ELSEIF Sales <=20000; Rate =.01; ELSE; Rate =.015; ENDIF; /END-FREE

Programming in RPG IV Third Edition 15 SELECT Appears on a line alone to identify the start of a case construct SELECT is followed by one or more WHENs –Each WHEN specifies a condition to be tested –Each WHEN is followed by one or more statements to be performed –As soon as it encounters a true condition, the computer executes the operation(s) following a WHEN Then control falls to ENDSL WHEN conditions can be coupled with AND/OR operations OTHER means “in all other cases” –OTHER should be the final catch all * /FREE SELECT; WHEN Sales <=5000; Rate =.005; WHEN Sales <=10000; Rate =.0075; WHEN Sales <=20000; Rate =.01; OTHER; Rate =.015; ENDSL; /END-FREE

Programming in RPG IV Third Edition 16 IF and Page Overflow Built in overflow indicators –OA thru OG and OV Use OFLIND File Specification keyword to associate indicator with report file Indicator will automatically come *ON when printer reaches overflow line at bottom of page

Programming in RPG IV Third Edition 17 Iteration Operations DOW Conditional expression - ENDDO DOU Conditional expression - ENDDO FOR - ENDFOR

Programming in RPG IV Third Edition 18 Do While Loop Leading decision loop Condition? Statement(s) Stop Start False True

Programming in RPG IV Third Edition 19 DOW Establishes a leading decision loop, based on a comparison –Allows AND and OR to form compound conditions Operations between DOW and ENDDO repeat as long as condition remains true * /FREE Number = 0; // Initialize Number to zero. Sum = 0; // Initialize Sum to zero. DOW Number < 100; // Loop while Number is less than 100. Number = Number + 1; // Increment Number by 1. Sum = Sum + Number; // Add Number to accumulator Sum. ENDDO; /END-FREE

Programming in RPG IV Third Edition 20 Do Until Loop Trailing decision loop Condition? Statement(s) Stop Start False True

Programming in RPG IV Third Edition 21 DOU Establishes a trailing decision loop, based on a comparison DOU repeats until the condition becomes true –Statements always execute at least once * /FREE Number = 0; // Initialize Number to zero. Sum = 0; // Initialize Sum to zero. DOU Number = 100; // Loop until Number equals 100. Number = Number + 1; // Increment Number by 1. Sum = Sum + Number; // Add Number to accumulator Sum. ENDDO; /END-FREE

Programming in RPG IV Third Edition 22 FOR Initiates a count-controlled loop Specify four things –Counter –Counter starting value Optional, defaults to 1 –Counter limit value (TO or DOWNTO) Optional, defaults to 1 –Counter increment value (BY) Optional, defaults to 1 * /FREE Sum = 0; FOR Number = 1 TO 100; Sum = Sum + Number; ENDFOR; /END-FREE

Programming in RPG IV Third Edition 23 Early Exits from Loops ITER skips the remaining instructions in the loop and causes the next repetition to begin LEAVE terminates the loop –Sends control to statements following ENDDO * /FREE DOW NOT %EOF; READ CustFile; IF %EOF; LEAVE; ELSE; EXCEPT Detail; ITER; ENDIF; ENDDO; /END-FREE

Programming in RPG IV Third Edition 24 Subroutines Subroutine: a block of code with an identifiable beginning and end Subroutines must have a unique name formed with the same rules that apply to fields Subroutines may execute other subroutines, but a subroutine should never execute itself Subroutines cannot contain other subroutines Subroutines appear after other calculation specifications –Organize subroutines alphabetically

Programming in RPG IV Third Edition 25 BEGSR/ENDSR BEGSR begins a subroutine –Include subroutine name ENDSR ends a subroutine * /FREE BEGSR CalcTax; EVAL(H) FICA = Gross *.0751; EVAL(H) StateTax = Gross *.045; IF Gross > 5000; EVAL(H) FedTax = Gross *.31; ELSE; EVAL(H) FedTax = Gross *.25; ENDIF; ENDSR; /END-FREE

Programming in RPG IV Third Edition 26 EXSR Executes subroutine named in Factor 2 * /FREE EXSR CalcTax; // EXSR causes control to drop to subroutine CalcTax. // Control returns here when the subroutine finishes.... BEGSR CalcTax; EVAL(H) FICA = Gross *.0751; EVAL(H) StateTax = Gross *.045; IF Gross > 5000; EVAL(H) FedTax = Gross *.31; ELSE; EVAL(H) FedTax = Gross *.25; ENDIF; ENDSR; /END-FREE

Programming in RPG IV Third Edition 27 Points to Remember Structured program design means developing program logic with flow of control tightly managed –Generally by using only structured operations Top-down methodology requires hierarchical program design –Work out broad logic first –Later, attend to detailed processing

Programming in RPG IV Third Edition 28 Points to Remember Decision logic operations –IF / ELSE / ELSEIF –SELECT / WHEN / OTHER Iteration (looping) operations –DOW (leading decision) –DOU (trailing decision) –FOR (counter controlled) Use ITER and LEAVE to provide an early exit from a loop Most of the structured operation of RPG require the use relational operators (>, =, <>)

Programming in RPG IV Third Edition 29 Points to Remember Subroutines allow an identifiable block of code to be executed BEGSR and ENDSR begin and end a subroutine –Coded after other calculations EXSR executes a subroutine