Control Structures with Pseudocode Control Structures Any - - PDF document
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
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 instruction
after another in the order in which they appear with no decisions involved.
No indentation, all statements
line up on the left.
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 =
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
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
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
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
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 =
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
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
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
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:
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
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
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
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
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
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
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
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
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
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 =
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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