🖥️

Program Logic and Design: Pseudocode and Flowcharts

May 23, 2024

Program Logic and Design Lecture

Pseudocode

Definition

  • Pseudocode is an English-like representation of the logical steps it takes to solve a problem.
  • Similar to writing steps for making a cup of tea:
    • Plug in the kettle
    • Put the tea bag in the cup
    • Add water to the kettle
    • Wait for the kettle to boil
    • Add water to the cup
    • Remove the tea bag
    • Add milk or sugar based on preferences
    • Serve

Characteristics

  • Programs begin with start and end with stop (can also use begin and end).
  • Each program statement performs one action (Input, Processing, Output).
  • Program statements are indented a few spaces more than the start or module name.
  • Each program statement appears on a single line if possible; continuation lines are indented.
  • Program statements begin with lowercase letters to avoid auto-capitalization issues.
  • No punctuation is used to end statements.
  • Example of pseudocode for doubling a number: start input my number set my answer = my number * 2 output my answer stop

Flowcharts

Definition

  • Flowchart is a pictorial representation of the logical steps it takes to solve a problem.
  • Example: Fixing a lamp
    • Lamp doesn't work -> Is it plugged in? -> Yes/No -> Next steps (Plug in, Check bulb, etc.)

Characteristics

  • Use various shapes to represent different types of statements:
    • Input/Output: Parallelogram
    • Processing: Rectangle
    • Start/Stop: Lozenge (race track shape)
  • Flow lines (arrows) are used to show the direction of the process.

Compare Pseudocode and Flowcharts

  • Think of them as maps (written directions vs. drawn maps).
  • Client preferences generally favor flowcharts for their pictorial nature.
  • Learning tool preference between pseudocode and flowcharts varies; studies show a majority of students prefer flowcharts.

Repetition (Loops)

Issue with Repetition

  • Initial pseudocode/flowchart for doubling a number is only feasible for one number, not for large repetitions (e.g., 10,000 times).
  • Infinite loops should be avoided as they never end.

Solution: Loops

  • Create a loop to repeat a series of steps instead of writing redundant lines of code.
    • Example: Flowchart indicates repetition by having a flow line that loops back to previous steps.
  • Upcoming topic: Using sentinel values to end loops.