Structured Programming Notes

Programming Models

Older Programming Languages (assembly)

  • Required programmers to work directly with memory addresses and machine language codes
  • Older languages were written in one piece
  • Not structured

Newer Structured and Object-Oriented Languages

  • More like natural language and use named variables instead of memory addresses
  • Allow the development of smaller modules that are reusable and connectable in different ways.
  • Problems are broken down into smaller pieces and teams of programmers work on the separate modules.

Procedural Programming

  • Focuses on procedures or functions that perform actions
  • Problems are broken down in to subtasks.
  • Example: a payroll program could be broken into modules:
    • to compute withholdings
    • to format and print paychecks u to deduct benefits costs

Object-oriented Programming

  • Focuses on objects or “things” and describes their features (attributes) and behaviors.
  • Modules are developed by object u Example: a payroll application would
  • Example: a payroll application would have:
    • employee objects with behaviors such as selecting benefits
    • paycheck objects with behaviors such as paychecks being printed and determining deductions

Assignment Statements

  • Setting a value into a variable
    • set myAnswer = myNumber *2
  • In this assignment statement, there are two actions taking place
    • First the computer calculates the value of myNumber *2
    • Second the computer stores the computed value in myAnswer
  • The equal sign is the assignment operator
    • set someNumber = 2
    • set someNumber = 3 * 7
    • set someNumber = someOtherNumber

Arithmetic Operations

+ (plus sign) – Addition

– (minus sign) – Subtraction * (asterisk) – Multiplication

/ (slash) – Division

  • Consider rules of precedence and order of operations and left-to-right associativity
  • Use parentheses to clarify the order you want

Modularization

  • Break down a program into units called modules (other names are procedures, functions, methods or subroutines)
  • The process of breaking down a large program into modules is called modularization
  • Why do we do this?
    • Modularization provides abstraction
    • Modularization allows multiple programmers to work on a problem
    • Modularization allows you to reuse your work more easily

Abstraction is the process of paying attention to important properties while ignoring non-essential details.

With Abstraction

  • Do laundry
  • Call mom
  • Start programming assignment

Without abstraction

  • Pick up laundry basket
  • Put laundry basket in car u Drive to laundromat
  • Get out of car with basket u Walk into laundromat
  • Set basket down
  • etc.

Abstraction with Modules

  • Example: a payroll program calls a module computeFederalWithholdingTax()
  • You use this one statement in your Pseudocode

     start

          get hours, salary

          computeFederalWithholdingTax()

               print paycheck

     stop

  • The module may be very complicated – you can write it later, purchase commercial software, have someone else write it.
  • When planning the main program, you don’t care about the details

Modularization

  • Multiple programmers can work together by having each individual work on separate modules
  • Modularization promotes reusability
    • Create a library of well-tested, well-functioning modules that can be utilized in many different programs
    • Programs are more reliable when comprised of these modules
    • Reusability saves time and money

Modularizing a Program

  • Most programs consist of a main program which contains the basic steps or mainline logic of the program
  • The main program accesses the modules that provide the details. u Modules contain:
    • Header – includes the name and possibly descriptions of data that needs to be provided to the module
    • Body – all the statements in the module
    • Return statement – marks the end of the module and identifies when control returns to the main program
  • Modules names usually start with a verb like calculate or determine and when used in Pseudocode are followed by parentheses.

Without modularization

start

   Declarations

      string name

      num balance

   input name, balance

   output “ABC Manufacturing”

   output “ 47 Park Lane”

   output “Omro, WI 54963”

   output “Customer”, name

   output “Total”, balance

stop

With modularization

start

   Declarations

      string name

      num balance

   input name, balance

   displayAddressInfo()

   output “Customer”, name

   output “Total”, balance

stop

displayAddressInfo()

   output “ABC Manufacturing”

   output “47 Park Lane”

   output “Omro, WI 54963

return

Structured vs. Unstructured

  • Structured programs follow the rules of structured logic; unstructured programs do not
  • Unstructured programs are often referred to as spaghetti code because the logic is as difficult to follow as a single noodle of spaghetti on a plate.
  • All programs, no matter how complicated, can be constructed using one or more of only three structures
    • Sequence
    • Selection
    • Loop

Sequence Structure

  • Perform the first action, then the next then so on…
  • There is no branching, (no decision or loop)
  • Good example is driving directions

Download PDF