Professional Documents
Culture Documents
Nancy Stern Hofstra University Robert A. Stern: Nassau Community College
Nancy Stern Hofstra University Robert A. Stern: Nassau Community College
Nancy Stern Hofstra University Robert A. Stern: Nassau Community College
9th Edition
OBJECTIVES
To familiarize you with 1. The use of IF statements for selection. 2. The variety of formats and options available with the conditional statement. 3. The use of the EVALUATE statement with COBOL 85.
Strutured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
Selection Using a Simple IF Statement
A Review of Logical Control Structures Basic Conditional Statements Planning Conditional Statements with Pseudocode and Flowcharts How Comparisons Are Performed ASCII and EBCDIC Collating Sequences Ending Conditional Sentences with a Period or an END-IF Scope Terminator (COBOL 85) The CONTINUE or NEXT SENTENCE Clause
Strutured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
Selection Using Other Options of the IF Statement
Nested Conditional Compound Conditional Sign and Class Tests Negating Conditionals
CONTENTS
Using IF Statements to Determine Leap Years Condition-Names The COBOL 85 EVALUATE Statement: Using the Case Structure as an Alternative to Selection
The following performs two MOVE operations if AMT1 is equal to AMT2, and two ADD operations if AMT1 is not equal to AMT2:
Strutured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP
Omitting the scope terminator is permitted for all versions of COBOL as long as the IF sentence ends with a period. However, we recommend that you use scope terminators with COBOL 85 and omit periods except for the last statement in a paragraph.
Strutured COBOL Programming, Stern & Stern, 9th Edition
Coding Guidelines
Indenting The technique of indenting and coding each statement on a separate line makes reading the program easier, but it does not affect compilation or execution. When errors do occur, it is much easier to isolate and correct them when each line contains a single statement (versus all statements run together on a single line).
Strutured COBOL Programming, Stern & Stern, 9th Edition
Spaces Spaces Special characters Special characters a-z 0-9 A-Z A-Z 0-9 a-z
High
DEBUGGING TIP
Do not mix upper- and lowercase letters when entering data in fields. This reduces the risk that comparisons might give problematic results. As a convention, we recommend you use uppercase letters in all input fields as well as in instructions. Use lowercase letters only for comments.
Strutured COBOL Programming, Stern & Stern, 9th Edition
QUESTIONS?
SELF-TEST
What is wrong with the following statements (1-6) ? 1. IF A IS LESS THAN B ELSE ADD 1 TO END-IF
Strutured COBOL Programming, Stern & Stern, 9th Edition
GO TO CONTINUE
XX
SELF-TEST
2. IF A IS EQUAL TO '127' TO B ADD A END-IF
Solution: Since A is compared to a nonnumeric literal, it should be an alphanumeric field. But A is added to another field, which implies that it is numeric. Hence a data type mismatch exists. Although this may, in fact, produce the correct results (depending on the contents of A), it is inadvisable to make a comparison where one field or literal is Strutured COBOL Programming, nonnumeric andStern & Stern, 9th Edition is numeric. the other
SELF-TEST
3. IF A EQUALS B END-IF MOVE
1 TO A
SELF-TEST
4. IF A IS LESS THEN B MOVE END-IF
2 TO CODE1
Solution: When the words GREATER and LESS are used, the COBOL word that follows is THAN and not THEN.
Strutured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
5. IF C = D ELSE MOVE END-IF
MOVE 0 TO COUNTER.
100 TO COUNTER
Strutured COBOL Programming, Stern & Stern, 9th Edition Solution: There should be no period after
SELF-TEST
6. IF C = D MOVE 0 TO COUNTER ELSE NEXT
SENTENCE.
Solution: ELSE NEXT SENTENCE, although not incorrect, is unnecessary. Note that END-IF cannot be used with NEXT
Strutured COBOL Programming, Stern & Stern, 9th Edition
Compound Conditional
We have seen that selection and iteration structures provide programs with a great deal of logical control capability. The compound conditional offers even greater flexibility for selection and enables the IF statement to be used for more complex problems. With the compound conditional, the programmer can test for several Strutured COBOL Programming, one 9th Edition conditions withStern & Stern, statement.
Compound Conditional
By using OR in a compound conditional, any of the conditions specified causes execution of the statement(s). If none of the conditions is met, the computer executes either the ELSE clause, if coded, or the next sentence. Any number of conditions separated by ORs may be coded in a single statement.
Strutured COBOL Programming, Stern & Stern, 9th Edition
This anomaly has Y2K ramifications. If two-digit year dates are still being used, then a year of 00 will be considered 1900 unless some adjustment is made. With COBOL 85, we can determine if any year is a leap year by Edition using an IF Strutured COBOL Programming, Stern & Stern, 9th statement.
CONDITION-NAMES
A condition-name is a user-defined word established in the DATA DIVISION that gives a name to a specific value that an identifier can assume. An 88-level entry coded in the DATA DIVISION is a condition-name that denotes a possible value for an identifier, which then can be tested to be either True or False.
A condition-name is always coded on the 88 level and has only a VALUE clause associated with it and will not contain a PICTUREProgramming, Stern & Stern, 9th Edition Strutured COBOL clause.
CONDITION-NAMES
Format for 88-level items: 88 condition-name VALUE literal The condition-name must be unique and its VALUE must be a literal consistent with the data type of the field preceding it: 05 CODE-IN 88 STATUS-OK
Strutured COBOL Programming, Stern & Stern, 9th Edition
CONDITION-NAMES
For readability, we indent each 88-level item to clarify its relationship to the data-name directly preceding it. Any elementary item on level numbers 01--49 in the FILE SECTION or in the WORKING-STORAGE may have a condition-name associated with it.
The COBOL 85 EVALUATE Statement: Using the Case Structure as an Alternative to Selection
CHAPTER SUMMARY
A. Simple Relational for Selection 1. Relations IS {EQUAL} {=} TO IS {LESS THAN} {<} IF identifier-1 IS {GREATER THAN} {>} identifier-2 IS {LESS THAN OR EQUAL TO} {<=}* IS {GREATER THAN OR EQUAL TO} {>=}* *COBOL 85 only.
Strutured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
2. If the condition exists, all statements are executed up to (a) the ELSE clause or (b) the END-IF (COBOL 85) or the period if there is no ELSE clause. 3. If the condition does not exist, the statements after the word ELSE, if coded, are executed, or (if there is no ELSE clause) processing continues after the END-IF or with the next sentence.
Strutured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
4. Comparisons are algebraic or logical: (1) Numeric: 12.0 = 12.00 = 12 = +12 (2) Nonnumeric: ABC = ABCb = ABCbb (b = blanks) 5. Collating sequences (EBCDIC and ASCII) are the same with regard to A--Z, 0--9, and a--z. They differ when upper- and lowercase letters are compared or when letters and digits are compared.
Strutured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
With ASCII, lowercase letters are greater than uppercase letters; With EBCDIC, lowercase letters are less than uppercase letters. With EBCDIC, letters are less than numbers. With ASCII, numbers are less than letters.
Strutured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
B. Other Types of IF Statements 1. Compound Condition a. Format IF condition-1 OR condition-2 . . . IF condition-1 AND condition-2 . . .
CHAPTER SUMMARY
b. Hierarchy (1) If ORs and ANDs are used in the same sentence, ANDs are evaluated first from left to right, followed by ORs. (2) Parentheses can be used to override hierarchy rules.
CHAPTER SUMMARY
2. Other Tests a. Sign test IF identifier-1 IS {POSITIVE} {NEGATIVE} {ZERO}
Identifier-1 must have an S in its PIC clause if it is to store data with a negative value.
Strutured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
b. Class Test IF identifier-1 IS {NUMERIC} {ALPHABETIC} c. Negated Conditionals (1) A test can be preceded with NOT to test the negative conditional. (2) IF NOT (A = B OR A = C) is the Strutured COBOL Programming, Stern & Stern, 9th IF A NOT = B AND Edition same as
CHAPTER SUMMARY
C. Condition-Names 1. Coded on the 88-level directly following the field to which it relates. For example: 05 CODE-IN 88 OK-CODE PIC X. VALUE '6'.
CHAPTER SUMMARY
2. A condition-name specifies a condition in the PROCEDURE DIVISION. For example: IF OK-CODE PERFORM 200-OK-RTN END-IF D. The COBOL 85 EVALUATE statement is often used as an alternative to nested IFs or a series of IF statements.
Strutured COBOL Programming, Stern & Stern, 9th Edition