Control Structures with Pseudocode Control Structures Any - - PDF document

control structures with pseudocode control structures
SMART_READER_LITE
LIVE PREVIEW

Control Structures with Pseudocode Control Structures Any - - PDF document

Control Structures with Pseudocode Control Structures Any computer-oriented problem can be solved using the following three structures: Sequence Decision or Selection Repetition or Iteration Sequence Carrying out one


slide-1
SLIDE 1

Control Structures with Pseudocode

slide-2
SLIDE 2

Control Structures

 Any computer-oriented

problem can be solved using the following three structures:

  • Sequence
  • Decision or Selection
  • Repetition or Iteration
slide-3
SLIDE 3

Sequence

 Carrying out one instruction

after another in the order in which they appear with no decisions involved.

 No indentation, all statements

line up on the left.

slide-4
SLIDE 4

Sequence Example

WRITE “Enter number: ” READ number MULTIPLY number by 50 and store in result PRINT result with label Test Data 10 number result Output

slide-5
SLIDE 5

Sequence Example

WRITE “Enter number: ” READ number MULTIPLY number by 50 and store in result PRINT result with label Test Data 10 number result Output Enter number: 10

slide-6
SLIDE 6

Sequence Example

WRITE “Enter number: ” READ number MULTIPLY number by 50 and store in result PRINT result with label Test Data 10 number result Output 10 Enter number: 10

slide-7
SLIDE 7

Sequence Example

WRITE “Enter number: ” READ number MULTIPLY number by 50 and store in result PRINT result with label Test Data 10 number result Output 10 500 Enter number: 10

slide-8
SLIDE 8

Sequence Example

WRITE “Enter number: ” READ number MULTIPLY number by 50 and store in result PRINT result with label Test Data 10 number result Output 10 500 Enter number: 10 Result is 500

slide-9
SLIDE 9

Decision or Selection

 Represents the computer’s

ability to make decisions.

 The structure presents a

condition which, when evaluated to true false, leads to the correct path.

 IF, ELSE  CASE  Indent all statements which fall

within the selection.

 Always use an END tag

slide-10
SLIDE 10

Decision or Selection

IF condition statement(s) if true ELSE statement(s) if false END IF

 Only one part is executed,

depending on the result of testing the condition.

 There are many variations.

slide-11
SLIDE 11

Simple IF Example

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test Data Test 1: 100 Test 2: 500 Test 3: 1000 balance serviceCharge interest OUTPUT

slide-12
SLIDE 12

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 OUTPUT

slide-13
SLIDE 13

Simple IF Example – T est 1

READ balance IF balance >= 500 false serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 OUTPUT

slide-14
SLIDE 14

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 OUTPUT

slide-15
SLIDE 15

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 5 OUTPUT

slide-16
SLIDE 16

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 5 0 OUTPUT

slide-17
SLIDE 17

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 5 0 OUTPUT

slide-18
SLIDE 18

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 5 0 95 OUTPUT

slide-19
SLIDE 19

Simple IF Example – T est 1

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 1: 100 balance serviceCharge interest 100 5 0 95 OUTPUT 95

slide-20
SLIDE 20

Simple IF Example – T est 2

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 OUTPUT

slide-21
SLIDE 21

Simple IF Example – T est 2

READ balance IF balance >= 500 true serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 OUTPUT

slide-22
SLIDE 22

Simple IF Example – T est 2

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 0 OUTPUT

slide-23
SLIDE 23

Simple IF Example – T est 2

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 0 15 OUTPUT

slide-24
SLIDE 24

Simple IF Example – T est 2

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 0 15 OUTPUT

slide-25
SLIDE 25

Simple IF Example – T est 2

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 0 15 515 OUTPUT

slide-26
SLIDE 26

Simple IF Example – T est 2

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 2: 500 balance serviceCharge interest 500 0 15 515 OUTPUT 515

slide-27
SLIDE 27

Simple IF Example – T est 3

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 OUTPUT

slide-28
SLIDE 28

Simple IF Example – T est 3

READ balance IF balance >= 500 true serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 OUTPUT

slide-29
SLIDE 29

Simple IF Example – T est 3

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 0 OUTPUT

slide-30
SLIDE 30

Simple IF Example – T est 3

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 0 30 OUTPUT

slide-31
SLIDE 31

Simple IF Example – T est 3

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 0 30 OUTPUT

slide-32
SLIDE 32

Simple IF Example – T est 3

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 0 30 1030 OUTPUT

slide-33
SLIDE 33

Simple IF Example – T est 3

READ balance IF balance >= 500 serviceCharge = 0 interest = balance * .03 ELSE serviceCharge = 5 interest = 0 END IF balance = balance + interest – serviceCharge PRINT balance Test 3: 1000 balance serviceCharge interest 1000 0 30 1030 OUTPUT 1030

slide-34
SLIDE 34

Nested IF Example

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 Test 2: 0 Test 3: -3 number Output

slide-35
SLIDE 35

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

slide-36
SLIDE 36

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

slide-37
SLIDE 37

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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

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

slide-40
SLIDE 40

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

slide-41
SLIDE 41

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

slide-42
SLIDE 42

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

slide-43
SLIDE 43

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

slide-44
SLIDE 44

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

slide-45
SLIDE 45

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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

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
slide-48
SLIDE 48

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
slide-49
SLIDE 49

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
slide-50
SLIDE 50

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
slide-51
SLIDE 51

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
slide-52
SLIDE 52

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
slide-53
SLIDE 53

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
slide-54
SLIDE 54

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.

slide-55
SLIDE 55

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

slide-56
SLIDE 56

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

slide-57
SLIDE 57

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

slide-58
SLIDE 58

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

slide-59
SLIDE 59

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

slide-60
SLIDE 60

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

slide-61
SLIDE 61

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

slide-62
SLIDE 62

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

slide-63
SLIDE 63

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

slide-64
SLIDE 64

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

slide-65
SLIDE 65

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

slide-66
SLIDE 66

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

slide-67
SLIDE 67

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

slide-68
SLIDE 68

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

slide-69
SLIDE 69

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

slide-70
SLIDE 70

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

slide-71
SLIDE 71

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

slide-72
SLIDE 72

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

slide-73
SLIDE 73

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

slide-74
SLIDE 74

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

slide-75
SLIDE 75

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

slide-76
SLIDE 76

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

slide-77
SLIDE 77

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

slide-78
SLIDE 78

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

slide-79
SLIDE 79

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

slide-80
SLIDE 80

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

slide-81
SLIDE 81

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

slide-82
SLIDE 82

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

slide-83
SLIDE 83

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

slide-84
SLIDE 84

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.

slide-85
SLIDE 85

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.

slide-86
SLIDE 86

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

slide-87
SLIDE 87

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

slide-88
SLIDE 88

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

slide-89
SLIDE 89

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

slide-90
SLIDE 90

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

slide-91
SLIDE 91

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

slide-92
SLIDE 92

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

slide-93
SLIDE 93

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

slide-94
SLIDE 94

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

slide-95
SLIDE 95

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

slide-96
SLIDE 96

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

slide-97
SLIDE 97

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

slide-98
SLIDE 98

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

slide-99
SLIDE 99

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 =

slide-100
SLIDE 100

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

slide-101
SLIDE 101

LOOP WHILE - T est 2

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 2: 5 num Output 5

slide-102
SLIDE 102

LOOP WHILE - T est 2

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 2: 5 num Output 5

slide-103
SLIDE 103

LOOP WHILE - T est 2

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 2: 5 num Output 5 num = 5

slide-104
SLIDE 104

LOOP WHILE - T est 2

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 2: 5 num Output 5 num = 5 6

slide-105
SLIDE 105

LOOP WHILE - T est 2

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 2: 5 num Output 5 num = 5 6

slide-106
SLIDE 106

LOOP WHILE - T est 2

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 2: 5 num Output 5 num = 5 6

slide-107
SLIDE 107

LOOP WHILE - T est 2

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 2: 5 num Output 5 num = 5 6 After the loop num =

slide-108
SLIDE 108

LOOP WHILE - T est 2

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 2: 5 num Output 5 num = 5 6 After the loop num = 6

slide-109
SLIDE 109

LOOP WHILE - T est 3

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 3: 7 num Output 7

slide-110
SLIDE 110

LOOP WHILE - T est 3

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 3: 7 num Output 7

slide-111
SLIDE 111

LOOP WHILE - T est 3

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 3: 7 num Output 7

slide-112
SLIDE 112

LOOP WHILE - T est 3

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 3: 7 num Output 7 After the loop num =

slide-113
SLIDE 113

LOOP WHILE - T est 3

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 3: 7 num Output 7 After the loop num = 7

slide-114
SLIDE 114

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice ” PRINT “(Continue or Quit): ” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output

slide-115
SLIDE 115

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice ” PRINT “(Continue or Quit): ” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output

slide-116
SLIDE 116

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice ” PRINT “(Continue or Quit): ” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Enter a choice:

slide-117
SLIDE 117

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Enter a choice (Continue or Quit): Continue

slide-118
SLIDE 118

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): Continue

slide-119
SLIDE 119

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” true ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): Continue

slide-120
SLIDE 120

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Continue

slide-121
SLIDE 121

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Continue

Enter a choice

slide-122
SLIDE 122

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Continue

Enter a choice (Continue or Quit):

Quit

slide-123
SLIDE 123

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Quit Continue

Enter a choice (Continue or Quit):

Quit

slide-124
SLIDE 124

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” false ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Quit Continue

Enter a choice (Continue or Quit):

Quit

slide-125
SLIDE 125

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Quit Continue

Enter a choice (Continue or Quit):

Quit

slide-126
SLIDE 126

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Quit Continue

Enter a choice (Continue or Quit):

Quit Times through loop =

slide-127
SLIDE 127

Trailer Controlled Loop

SET counter to 0 PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice LOOP WHILE choice not = “Quit” ADD 1 to counter PRINT “Enter a choice:” PRINT “Continue or Quit” READ choice END LOOP DISPLAY “Times through loop = ” DISPLAY counter counter choice Output 0 Continue Enter a choice (Continue or Quit): 1 Quit Continue

Enter a choice (Continue or Quit):

Quit Times through loop = 1

slide-128
SLIDE 128

Loop Points to Remember

 The test value (such as reading a

data value) must be initialized before the condition is tested.

 Be sure the test value changes

within the loop so the test condition will eventually test false to make the loop stop. Otherwise an endless loop results.

initialize LOOP WHILE condition change condition test value END LOOP

 It is possible the statements in

the loop are never executed.

slide-129
SLIDE 129

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count numberYears END LOOP PRINT amountOwed Test Data 100

interestRate numberYears amountOwed OUTPUT

slide-130
SLIDE 130

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count numberYears END LOOP PRINT amountOwed Test Data 100 interestRate numberYears amountOwed 0.1 OUTPUT

slide-131
SLIDE 131

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count numberYears END LOOP PRINT amountOwed Test Data 100 interestRate numberYears amountOwed 0.1 2 OUTPUT

slide-132
SLIDE 132

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count numberYears END LOOP PRINT amountOwed Test Data 100 interestRate numberYears amountOwed 0.1 2 100 OUTPUT

slide-133
SLIDE 133

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears true ADD interestRate times amountOwed to amountOwed ADD 1 to count numberYears END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 OUTPUT

slide-134
SLIDE 134

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 110 OUTPUT

slide-135
SLIDE 135

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 OUTPUT

slide-136
SLIDE 136

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears true ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 OUTPUT

slide-137
SLIDE 137

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 121 OUTPUT

slide-138
SLIDE 138

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 2 121 OUTPUT

slide-139
SLIDE 139

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears false ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 2 121 OUTPUT

slide-140
SLIDE 140

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 2 121 OUTPUT

slide-141
SLIDE 141

LOOP # times

SET interestRate to 0.1 SET numberYears to 2 READ amountOwed LOOP count starts at 0 to < numberYears ADD interestRate times amountOwed to amountOwed ADD 1 to count END LOOP PRINT amountOwed Test Data 100 interestRate numberYears count amountOwed 0.1 2 0 100 1 110 2 121 OUTPUT 121