Chapter 8 Decision Making Using the IF and EVALUATE Statements.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Copyright © Cengage Learning. All rights reserved. 6 Equations and Formulas.
8-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
Chapter 3 Making Decisions
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Conditionals & boolean operators
Lesson NUMERIC-FIELDPIC 9(5). 05 ALPHANUMERIC-FIELDPIC X(5).... IF NUMERIC-FIELD = 10...(valid entry) IF NUMERIC-FIELD = ‘10’... (invalid entry)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
5-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Chapter 5: Making Decisions
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
Chapter 8 – Data Validation
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Chapter 2 Inequalities. Lesson 2-1 Graphing and Writing Inequalities INEQUALITY – a statement that two quantities are not equal. SOLUTION OF AN INEQUALITY.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
Decision Making and Branching
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Order of Operations PEMDAS DON’T STRESS. Numerical Expression : A mathematical phrase that includes numerals and operational symbols. It does not have.
© 2010 Pearson Prentice Hall. All rights reserved. CHAPTER 5 Number Theory and the Real Number System.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 Type Called bool  Bool has only two possible values: True and False.
5.04 Apply Decision Making Structures
Java Programming Fifth Edition
More on the Selection Structure
AND.
Exponents and Order of Operations
Truth Tables for Negation, Conjunction, and Disjunction
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
2-1 Making Decisions Sample assignment statements
Objectives After studying this chapter, you should be able to:
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter 5: Control Structure
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
SELECTIONS STATEMENTS
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Relational Operators.
Section 3.2 Truth Tables for Negation, Conjunction, and Disjunction
Chapter 3: Selection Structures: Making Decisions
Lecture 9: Implementing Complex Logic
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

Chapter 8 Decision Making Using the IF and EVALUATE Statements

COBOL Statements Two categories Conditional statements –Performs operations depending on existence of some condition –Coded with IF-THEN-ELSE structure Imperative statements –Performs operation regardless of existing conditions –MOVE, ADD are examples in COBOL

IF Statement IF condition-1 [THEN] imperative statement-1 … [ELSE imperative statement-2 …] [END-IF] Format

ELSE is Optional May be omitted if operation required only when condition exists If Acct-Balance < 0 Then Display 'Account overdrawn' End-If DISPLAY executed if Acct-Balance less than zero, otherwise it is ignored

Four Types of IF Statements Relation Condition –IF WS-LINE-COUNT EQUAL 53 Class Condition –IF WS-PAY-RATE IS NUMERIC Sign Condition –IF QTY-ON-HAND IS POSITIVE Condition-name –IF END-OF-INPUT-FILE

Relational Operators Symbols for simple relational conditions SymbolMeaning <is less than >is greater than =is equal to <=less than or equal to >=greater than or equal to

Nested Conditional IF statement itself can contain additional IF statements Pair each IF with an END-IF Used when more than two conditions need to be tested

Compound Conditional To test for several conditions with one statement Code multiple conditions separated by ORs or ANDs

OR Compound Conditional Use OR to test whether any one of several conditions exists IF A = B OR B > 12 Add A TO Total ELSE ADD 1 TO Count END-IF Executed if either condition exists Executed only if A not = B and B <= 12

AND Compound Conditional Use AND to test if all of several conditions are met IF A = 5 AND B > 0 ADD 10 TO A ELSE MOVE 0 TO B END-IF Executed if both simple conditions met Executed if one or both simple conditions not met

AND and OR in Conditionals Compound conditions may include both AND and OR Hierarchy rules –Conditions with AND evaluated first from left to right –Conditions with OR evaluated last from left to right –Parentheses used to override this order

AND and OR in Conditionals If Q > 0 Or R < S And R = 10 Multiply 2 By Q End-If Test conditions in this order: 1. R < S And R = 10 OR 2. Q > 0 Example

Negating Conditionals NOT placed before conditional reverses its truth value ConditionResult If Amt Not = 10 True if Amt is 15 False if Amt is 10 If Amt Not > 8 True if Amt is 2 False if Amt is 12

Negating Compound Conditionals To negate compound conditional place it in parentheses, precede it with NOT Condition to check for In-Code of S or D If In-Code = 'S' Or In-Code = 'D' To negate this condition (check for In- Code that is neither S nor D) If Not (In-Code = 'S' Or In-Code = 'D')

Sign Tests To test whether field is POSITIVE, NEGATIVE or ZERO ConditionResult If Amt Is Positive True if Amt is greater than 0 If Amt Is Negative True if Amt is less than 0 If Amt Is Zero True if Amt equals 0

Class Test To test whether type of data if field is numeric or alphabetic ConditionResult If Amt Is Numeric True if Amt = 153 False if Amt = 15B If Code Is Alphabetic True if Code = PQR False if Code = P23

Defining Condition-Names 05Pay-CodePic X. 88HourlyValue 'H'. 88SalariedValue 'S'. Define field in DATA DIVISION Use level 88 to define condition-name and associated value Example

Using Condition-Names Use any place a condition can be used in PROCEDURE DIVISION If Hourly Perform Calc-Hourly-Pay End-If If Pay-Code field has a value of 'H', condition Hourly is true Hourly same as condition Pay-Code='H'

EVALUATE Statement Used to implement Case structure Tests for series of conditions May be used in place of IF statement Often code clearer, more efficient with EVALUATE when multiple condition need to be checked

EVALUATE Statement identifier-1 EVALUATE expression-1 WHEN condition-1 imperative-statement-1 … [WHEN OTHER imperative-statement-2 …] [END-EVALUATE] Format

The EVALUATE Statement EVALUATE STU-CLASS WHEN “FR” PERFORM 110-FRESHMAN WHEN “SO” PERFORM 120-SOPHOMORE WHEN “JR” PERFORM 130-JUNIOR WHEN “SR” PERFORM 140-SENIOR WHEN “GS” PERFORM 150-GRADUATE WHEN OTHER PERFORM 199-ERROR END-EVALUATE

Decision Table Often used to list conditions and actions to be performed

Code for Decision Table If Code = 'T' If N > 10 Multiply.15 By N Else Multiply.25 By N End-If Else Move 0 To N End-If Delimits inner IF Delimits outer IF