Presentation is loading. Please wait.

Presentation is loading. Please wait.

LOGICAL CONTROL STRUCTURES (chp. 8)

Similar presentations


Presentation on theme: "LOGICAL CONTROL STRUCTURES (chp. 8)"— Presentation transcript:

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

2 IF STATEMENTS Examples: Syntax Definition: IF condition-1 [THEN]
For Your Information A condition should evaluate to TRUE or FALSE. If the condition is true, the statements are executed. 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 Syntax Definition: IF condition-1 [THEN] statement(s)-1 [ELSE statement(s)-2] [END-IF] Conditions: Relational operators: <, >, <=, >=, = Logical operators: AND, OR, NOT Reminder: left right AND OR NOT left T T T T F T F F T F T F T T F F F F Examples: See example code Statement Syntax

3 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

4 MORE ON CONDITIONS COLLATING SEQUENCE – EBCDIC (ibm) and ASCII (unix)
LOW spaces spaces special characters special characters a-z 0-9 A-Z A-Z HIGH a-z 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 (true ….. false) a > c or b > c (true ….. false) a = b and b = c and a = d (true ….. false) a = b or b = c or a = d (true ….. false) a = b or b = c and a = d (true ….. false) a = b and b = c or a = d (true ….. false) Examples “smith” _____ “saunders” “Smith” _____ “saunders” “hi” ________ “hello” “YES” ______ “yes” “abc123” ______ “123abc” “aBc” _______ “AbC” Statement Syntax

5 IMPLIED CONDITIONS OTHER!
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 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

6 CONDITION NAMES Syntax Definition: 88 condition-name VALUE literal
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 working-storage section of the data division. 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. 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. 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 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. Statement Syntax

7 EVALUATE STATEMENTS Examples: Syntax Definition: EVALUATE TRUE
{WHEN condition-1 stmt-group-1…} [WHEN OTHER stmt-group-2] [END-EVALUATE] Examples: 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 For Your Information Equivalent to nested IF structures. Only the first condition that is true is executed (like an IF structure!). 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

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


Download ppt "LOGICAL CONTROL STRUCTURES (chp. 8)"

Similar presentations


Ads by Google