control structures with pseudocode control structures
play

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


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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.

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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.

  51. 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.

  52. 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

  53. 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

  54. 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

  55. 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

  56. 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

  57. 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

  58. 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

  59. 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

  60. 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

  61. 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

  62. 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

  63. 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

  64. 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

  65. 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 =

  66. 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