Nested IF – T est 1 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 1: 5 number Output 5
Nested IF – T est 1 READ number IF number > 0 true DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 1: 5 number Output 5
Nested IF – T est 1 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 1: 5 number Output 5 Positive
Nested IF – T est 1 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 1: 5 number Output 5 Positive
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0
Nested IF – T est 2 READ number IF number > 0 false DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 1: 5 number Output 0
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 false DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0 Zero
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0 Zero
Nested IF – T est 2 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 2: 0 number Output 0 Zero
Nested IF – T est 3 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3
Nested IF – T est 3 READ number IF number > 0 false DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3
Nested IF – T est 3 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3
Nested IF – T est 3 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 true DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3
Nested IF – T est 3 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3 Negative
Nested IF – T est 3 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3 Negative
Nested IF – T est 3 READ number IF number > 0 DISPLAY “Positive” ELSE IF number < 0 DISPLAY “Negative” ELSE DISPLAY “Zero” END IF END IF T est Data Test 3: -3 number Output -3 Negative
CASE Structure An alternative way of representing nested IFs. Used most often where there are more than two possible choices. Easier to write and understand than a nested IF. Guidelines: ◦ 2 choices, use IF ◦ 3 choices, use IF or CASE ◦ > 3 choices, use CASE Conditions are tested one after another until a true condition is found. Only one set of actions is executed, the actions of the true condition. After execution of the true condition, control jumps to the end, skipping all of the remaining tests. The OTHERWISE is optional, but it is a good programming habit to always include it. It is often used for an error message when none of the conditions are true.
CASE Structure CASE variable WHEN value (literal or variable) statement(s) if true WHEN value statement(s) if true OTHERWISE statement(s) if all other cases are false END CASE
CASE Example READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 1: 1 Test 2: 2 Test 3: 3 Test 4: 10 choice Output
CASE Example – T est 1 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 1: 1 choice Output 1
CASE Example – T est 1 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 1: 1 choice Output 1
CASE Example – T est 1 READ choice CASE choice WHEN 1 true DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 1: 1 choice Output 1
CASE Example – T est 1 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 1: 1 choice Output 1 Choice was 1
CASE Example – T est 1 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 1: 1 choice Output 1 Choice was 1
CASE Example – T est 2 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 2: 2 choice Output 2
CASE Example – T est 2 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 2: 2 choice Output 2
CASE Example – T est 2 READ choice CASE choice WHEN 1 false DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 2: 2 choice Output 2
CASE Example – T est 2 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 true DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 2: 2 choice Output 2
CASE Example – T est 2 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 2: 2 choice Output 2 Choice was 2
CASE Example – T est 2 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 2: 2 choice Output 2 Choice was 2
CASE Example – T est 3 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3
CASE Example – T est 3 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3
CASE Example – T est 3 READ choice CASE choice WHEN 1 false DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3
CASE Example – T est 3 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 false DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3
CASE Example – T est 3 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 true DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3
CASE Example – T est 3 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3 Choice was 3
CASE Example – T est 3 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 3: 3 choice Output 3 Choice was 3
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4
CASE Example – T est 4 READ choice CASE choice WHEN 1 false DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 false DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 false DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4 Invalid choice
CASE Example – T est 4 READ choice CASE choice WHEN 1 DISPLAY “Choice was 1” WHEN 2 DISPLAY “Choice was 2” WHEN 3 DISPLAY “Choice was 3” OTHERWISE DISPLAY “Invalid choice” END CASE Test Data Test 4: 4 choice Output 4 Choice was 4
Repetition or Iteration Use in a situation where a single task must be performed several times. Loop The number of times it is processed depends on the condition that is evaluated each time the group is processed. Indent all statements which fall within the loop structure
Three Types of Repetition Structures 1. Pretest Loop LOOP WHILE condition statement(s) if true END LOOP ◦ Must initialize the test value before the condition can be tested. ◦ Be sure the test value changes so the condition will eventually tests false. ◦ IF the condition tests false the first time, the statements in the loop are never executed.
Three Types of Repetition Structures 2. Counter Controlled Loop LOOP # times statement(s) if true END LOOP ◦ Specialized type of pre-test loop. 3. Posttest Loop LOOP UNTIL condition statement(s) if true END LOOP ◦ Tested at end of the loop. ◦ Statements in the loop are always executed at least once. ◦ Loop terminates when the condition evaluates to true. ◦ Primarily used for searching through data for a specific value.
LOOP WHILE Example READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 Test 2: 5 Test 3: 7 num Output
LOOP WHILE - T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 true PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3
LOOP WHILE - T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 true PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 true PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5 num = 5
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5 num = 5 6
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 false PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5 num = 5 6
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5 num = 5 6
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5 num = 5 6 After the loop num =
LOOP WHILE – T est 1 READ num LOOP WHILE num <= 5 PRINT “num = ” num ADD 1 to num END LOOP DISPLAY “After the loop num = ” DISPLAY num Test data Test 1: 3 num Output 3 num = 3 4 num = 4 5 num = 5 6 After the loop num = 6
Recommend
More recommend