8 1 Chapter 8 To familiarize you with 1 IF statements for selection 2 Formats and options available with conditional statements 3 EVALUATE statement 8 2 Chapter Objectives Review of stuff youve completed ID: 626648
Download Presentation The PPT/PDF document "Decision Making Using the IF and EVALUAT..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Decision Making Using the IF and EVALUATE Statements
8-
1
Chapter 8Slide2
To familiarize you with
1. IF statements for selection2. Formats and options available with conditional statements3. EVALUATE statement
8-2
Chapter Objectives
Review of stuff you’ve completedSlide3
Selection Using Simple IF StatementSelection Using Other Options of IFUsing IF Statements to Determine Leap Years
Condition-NamesEVALUATE Statement: Using Case Structure as Alternative to Selection
8-3
Chapter ContentsSlide4
Two categoriesConditional statements
Performs operations depending on existence of some conditionCoded with IF-THEN-ELSE structureImperative statementsPerforms operation regardless of existing conditions
MOVE, ADD are examples in COBOL8-
4
COBOL StatementsSlide5
IF
condition-1 [THEN]
imperative statement-1 … [ELSE imperative statement-2 …] [END-IF]
8-
5
IF Statement
FormatSlide6
If condition exists or is trueStatement(s) after THEN executed
ELSE clause ignoredIf condition does not exist or is falseStatement(s) after ELSE executedStatement(s) after THEN ignored
Note that if you don’t use the optional END-IF you can terminate the IF statement with a period.
8-
6
IF StatementSlide7
If Disc-Code = 1 Then
Multiply Amt
By .15 Giving WS-DiscountElse Move 0 To WS-DiscountEnd-If
8-
7
IF Statement ExampleSlide8
May be omitted if operation required only when condition exists
If Acct-Balance < 0 Then
Display 'Account overdrawn' End-IfDISPLAY executed if Acct-Balance less than zero, otherwise it is ignored
8-
8
ELSE is OptionalSlide9
Symbols for simple relational conditions
Symbol Meaning < is less than
> is greater than = is equal to <= less than or equal to >= greater than or equal to
8-
9
Relational OperatorsSlide10
Assume L, M and N are numeric L = 12, M = 7, N = 3
Condition
Result L >= M True M < 7 False M > N + 6 False <- does the + first M + N <= 10 True
8-
10
Condition ExamplesSlide11
Compare fields to others fields or literals of same data typeNumeric fields compared algebraically
005 < 026 < 539 All of these considered equal 012 12.00 12 +12
8-11
How Comparisons PerformedSlide12
Nonnumeric fields compared alphabetically ABLE < BAKE < BARK
Blanks on right do not affect comparisonAll of these considered equal ABC ABCbb ABCbbbbb
8-12
How Comparisons PerformedSlide13
When alphanumeric field has mix of upper-, lower-case letters and digitsResult of comparison depends on collating sequence used on computer
Two types of internal codes to represent dataEBCDIC mainly on IBM mainframesASCII on PCs, minis, non-IBM mainframes
8-13
Collating SequencesSlide14
Collating Sequences
8-
14Slide15
8-
15
EBCDIC vs ASCII ComparisonSlide16
Used to indicate no operation should be performed when a condition exists
If Amt1 = Amt2 Then Continue
Else Add 1 to Total End-If
8-
16
CONTINUE clause
No operation performed if Amt1 = Amt2, continues with statement after End-IfSlide17
IF statement itself can contain additional IF statementsPair each IF with an END-IFUsed when more than two conditions need to be tested
8-
17
Nested ConditionalSlide18
If Code = 'T'
If N > 10 Multiply .15 By N Else
Multiply .25 By N End-IfElse Move 0 To N
End-If
8-
18
Code for Decision Table
Delimits inner IF
Delimits outer IFSlide19
To test for several conditions with one statementCode multiple conditions separated by ORs or ANDs
8-
19Compound ConditionalSlide20
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 CountEnd-If
8-
20
OR Compound Conditional
Executed if either condition exists
Executed only if A not = B and B <= 12Slide21
When same operand used in compound conditions, operand can be named onceIf X = 10 Or X = 20 may be written
If X = 10 Or 20Tests two simple conditions, X = 10, X = 20X is the implied operand
in the second condition test8-
21
Implied OperandsSlide22
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 BEnd-If
8-
22
AND Compound Conditional
Executed if both simple conditions met
Executed if one or both simple conditions
not
metSlide23
Compound conditions may include both AND and ORHierarchy rulesConditions with AND evaluated first from left to right
Conditions with OR evaluated last from left to rightParentheses used to override this order
8-23
AND and OR in ConditionalsSlide24
If Q > 0 Or R < S And R = 10
Multiply 2 By QEnd-If
Test conditions in this order: 1. R < S And R = 10 OR 2. Q > 0
8-
24
AND and OR in Conditionals
ExampleSlide25
To test whether field is POSITIVE, NEGATIVE or ZERO
Condition Result
If Amt Is Positive True if Amt is greater than 0If Amt Is Negative True if Amt is less
than 0
If Amt Is Zero True if Amt equals 0
8-
25
Sign TestsSlide26
To test type of data
Condition ResultIf Amt Is Numeric True if Amt = 153
False if Amt = 15BIf Code Is Alphabetic True if Code = PQR False if Code = 23
8-
26
Class TestSlide27
Reserved Word
MeaningALPHABETIC A-Z, a-z, and blankALPHABETIC-UPPER A-Z and blank
ALPHABETIC-LOWER a-z and blank8-
27
ALPHABETIC Class TestsSlide28
NOT placed before conditional reverses its truth value
Condition ResultIf Amt Not = 10 True if Amt is 15
False if Amt is 10If Amt Not > 8 True if Amt is 2 False if Amt is 12
8-
28
Negating ConditionalsSlide29
These two conditions are not the same
If Amt Is NegativeTrue if Amt is less than zeroIf Amt is Not Positive
True if Amt is less than or equal to zeroZero (0) is neither positive or negative
8-
29
Negating ConditionalsSlide30
These two conditions are not
the sameIf In-Code Is NumericTrue if Code is digits only
If In-Code Is Not AlphabeticTrue if In-Code contains any character that is not a letterField with combination of letters, digits and special characters is neither NUMERIC nor ALPHABETIC
8-
30
Negating ConditionalsSlide31
To negate compound conditional place it in parentheses, precede it with NOTCondition 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')8-
31
Negating Compound ConditionalsSlide32
May also use
DeMorgan's Rule to negate compound conditionsFor conditions separated by OR change OR to AND
and use NOT in each conditionCondition to check for In-Code that is neither S nor D may be stated as If Not In-Code = 'S' And Not In-Code = 'D‘
DeMOrgan’s
rule:
NOT (P AND Q) = (NOT P) OR (NOT Q)
NOT (P OR Q) = (NOT P) AND (NOT Q)
8-
32
Negating Compound ConditionalsSlide33
To negate conditions separated by AND change AND to OR and use NOT in each conditionCondition If A = B And C = D may be negated with either of these conditions
If Not (A = B And C = D)
If A Not = B Or C Not = D
8-
33
Negating Compound ConditionalsSlide34
Meaningful names defined for specific values that an identifier can assume
Associate names with employee pay code values
Pay-Code Condition-name H Hourly S Salaried
8-
34
Condition-Names
ExampleSlide35
05 Pay-Code Pic X.
88 Hourly Value 'H'. 88 Salaried Value 'S'.
Define field in DATA DIVISIONUse level 88 to define condition-name and associated valueUse these in Boolean expressions
8-
35
Defining Condition-Names
ExampleSlide36
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'
8-
36
Using Condition-NamesSlide37
Condition-name must be uniqueLiteral in VALUE clause must be same data type as field preceding it
May be coded with elementary items with level numbers 01-49
8-37
Using Condition-Names Slide38
88-level may specify multiple values
05 Opt-Num Pic 9. 88 Valid-Options Value 1 Thru 5
Valid-Options true if Opt-Num = 1, 2, 3, 4 or 58-
38
Using Condition-NamesSlide39
Used to implement Case structureTests for series of conditions
May be used in place of IF statementOften code clearer, more efficient with EVALUATE when multiple condition need to be checkedReminds me of the CASE or
SWITCH statement8-
39
EVALUATE StatementSlide40
identifier-1
EVALUATE
expression-1 WHEN condition-1 imperative-statement-1 … [
WHEN
OTHER
imperative-statement-2 …]
[
END-EVALUATE
]
8-
40
EVALUATE Statement
FormatSlide41
Add, subtract or multiply a number by 10 depending on value in Op-Code
Evaluate Op-Code
When 'A' Add 10 To Num When 'S' Subtract 10 From
Num
When 'M' Multiply 10 By
Num
When Other Display 'Code invalid'
End-Evaluate
Or call procedures after the test
8-
41
EVALUATE ExampleSlide42
When Op-Code is 'A' the ADD statement will be executedExecution will continue with statement after END-EVALUATE
If Op-Code is not A, S or M, statement following When Other is executed
8-42
EVALUATE StatementSlide43
Simple relational conditions use the operators =, <, >, <=, >=Simple IF Statement
If condition exists, all statements up to ELSE clause or END-IF are executedIf condition does not existStatements after ELSE are executed
Next statement after END-IF executed if no ELSE8-
43
Chapter SummarySlide44
8-
44Slide45
Comparisons made Algebraically for numeric fieldsUsing collating sequence for alphanumeric fields
Compound conditions join simple conditions with AND or ORANDs evaluated before Ors in order left to right
Parenthese used to override hierarchy rules8-
45
Chapter SummarySlide46
Other testsSign tests - POSITIVE, NEGATIVE, ZEROClass tests - NUMERIC, ALPHABETIC
Negated conditionals - may precede any test with NOT
8-46
Chapter SummarySlide47
Condition-names may be defined at 88 levelAssociates name with value a field may assume
Use name as condition in PROCEDURE DIVISIONEVALUATE often used as alternative to IF or series of nested IFs
8-47
Chapter Summary