LOGICAL CONTROL STRUCTURES chp 8 1 Sequence move

  • Slides: 8
Download presentation
LOGICAL CONTROL STRUCTURES (chp. 8) 1. Sequence – move, add, write, etc. Entry Function

LOGICAL CONTROL STRUCTURES (chp. 8) 1. Sequence – move, add, write, etc. Entry Function A Function B Exit 2. Selection – if true Entry Function A Exit condition false Function B 3. Iteration – perform condition Entry Function A true Exit false Statement Syntax 1

IF STATEMENTS For Your Information Syntax Definition: IF condition-1 [THEN] statement(s)-1 [ELSE statement(s)-2] [END-IF]

IF STATEMENTS For Your Information Syntax Definition: IF condition-1 [THEN] statement(s)-1 [ELSE statement(s)-2] [END-IF] A condition should evaluate to TRUE or FALSE. If the condition is true, the statements are executed. Conditions: Relational operators: <, >, <=, >=, = Logical operators: AND, OR, NOT Reminder: left right AND OR NOT left T T F T F T F T T F F If the condition is false, the ELSE section of statements is executed, if it exists. Remember, the ELSE is optional Relational operators have equivalent worded clauses. For example, “is less than” and “is greater than or equal to”. AND and OR go between two conditions that evaluate to true or false. Conditions are evaluated Examples: See example code Statement Syntax 2

CONDITIONS CONDITION-1 can be defined as: identifier-1 IS [NOT] { identifier-2 } GREATER THAN

CONDITIONS CONDITION-1 can be defined as: identifier-1 IS [NOT] { identifier-2 } GREATER THAN (>) LESS THAN (<) EQUAL TO (=) LESS THAN OR EQUAL TO (<=) GREATER THAN OR EQUAL TO (>=) { } NUMERIC ALPHABETIC POSITIVE NEGATIVE ZERO ALPHABETIC-UPPER ALPHABETIC-LOWER Statement Syntax 3

MORE ON CONDITIONS COLLATING SEQUENCE – EBCDIC (ibm) and ASCII (unix) LOW HIGH spaces

MORE ON CONDITIONS COLLATING SEQUENCE – EBCDIC (ibm) and ASCII (unix) LOW HIGH spaces special characters a-z A-Z 0 -9 spaces special characters 0 -9 Examples A-Z “smith” _____ “saunders” a-z “Smith” _____ “saunders” “hi” ____ “hello” “YES” ______ “yes” “abc 123” ______ “ 123 abc” “a. Bc” _______ “Ab. C” PRECEDENCE conditions surrounding AND conditions surrounding OR AND from left to right OR from left to right FYI: use parenthesis to override the above precedence Examples where a=1, b=2, c=2, d=3 a > c and b > c a > c or b > c a = b and b = c and a = b or b = c or a = d a = b or b = c and a = b and b = c or a = d (true …. . false) Statement Syntax 4

IMPLIED CONDITIONS The first test in a condition must be a “full test” meaning

IMPLIED CONDITIONS The first test in a condition must be a “full test” meaning it evaluates to TRUE or FALSE on its own The subsequent conditions can have implied operands and/or operators if a = b and a = c if a = b and c see example code… OTHER! Numeric/alphabetic is a CLASS test Positive/negative/zero is a SIGN test should have an S in the pic clause absolute value example (also in book) not negative is not equal to positive not greater than or equal to is equivalent to less than Statement Syntax 5

CONDITION NAMES Syntax Definition: 88 condition-name VALUE literal […] | [THRU | THROUGH literal]

CONDITION NAMES Syntax Definition: 88 condition-name VALUE literal […] | [THRU | THROUGH literal] Examples: 01 eof-switch pic xxx value “no”. 88 eof-cond value “yes”. perform para-1 until eof-cond. For Your Information Since a condition should evaluate to true or false, a condition name should also evaluate to true or false. Like all other level numbers, the condition name level number 88 also appears in the workingstorage section of the data division. 01 college-rank pic 9. 88 rank-fresh value 1. 88 rank-soph value 2. 88 rank-junior value 3. 88 rank-senior value 4. 88 under-grad value 1 2 3 4. If rank-fresh move “FR” to rank-desc. If rank-soph move “SO” to rank-desc…. If under-grad move “UG” to rank-course. If not under-grad move “G” to rank-course. Each condition name has an equivalent condition using the 01 level data name. For instance, for example 1: read input-file at end move “yes” to eof-switch. perform para-1 until eof-switch = “yes” For example 2: if college-rank = 1 move “FR” to rank-desc if college-rank = 1 or 2 or 3 or 4 move “UG” to rank-course. if college-rank > 4 move “G” to rank-course. Statement Syntax 6

EVALUATE STATEMENTS Syntax Definition: EVALUATE TRUE {WHEN condition-1 stmt-group-1…} [WHEN OTHER stmt-group-2] [END-EVALUATE] For

EVALUATE STATEMENTS Syntax Definition: EVALUATE TRUE {WHEN condition-1 stmt-group-1…} [WHEN OTHER stmt-group-2] [END-EVALUATE] For Your Information Examples: Only the first condition that is true is executed (like an IF structure!). Evaluate true when college-rank = 1 move “FR” to rank-desc move “UG” to rank-course when college-rank = 2… when college-rank = 3… when college-rank = 4… when college-rank > 4… when other perform 500 -error-routine End-evaluate Equivalent to nested IF structures. Can use condition names i. e instead of condition-1 being college-rank = 1, you can use rank-fresh. Other evaluate syntax definition… Statement Syntax 7

PERFORM STATEMENTS (chp. 9) For Your Information TYPES: Perform-Until Perform # Times Perform Varying

PERFORM STATEMENTS (chp. 9) For Your Information TYPES: Perform-Until Perform # Times Perform Varying “with test before” is the default. FROM is the initial value of identifier-1 BY is the modifying value for identifier-1 Syntax Definition: PERFORM [paragraph-name-1] {identifier-1 | integer-1} TIMES Initialize, test, execute, modify, test, execute, … identifier-1| integer-1 must be positive integer or zero PERFORM [paragraph-name-1] [ {THROUGH | THRU} paragraph-name-2 ] [WITH TEST {BEFORE | AFTER} ] [VARYING identifier-1 FROM { identifier-2 | integer-2} BY {identifier-3 | integer-3} ] UNTIL condition-1 identifier-2 | integer-2 and identifier-3|integer-3 can be positive or negative real values Move 1 to a. Perform until a > 10 display “a = “ a add 1 to a End-perform AFTER Init, exec, test Mod, exec, test, … Perform varying a from 1 by 1 until a > 10 display “a = “ a End-perform Q 1. What if “”with test after”? Q 2. What is value of “a” once loop has finished executing? ! Q 3. What if add/display in different order? Statement Syntax 8