Notes for Lesson 3: Fundamentals of Programming

Fundamentals of Programming — Lesson 3 Notes

  • Course context

    • Course: Fundamentals of Programming (IT) — taught by Jesfer Dela Cruz, Elsie Isip, John Ivan Maurat, Cyrus RJ Robles, Leonila Valdez; Manual ver 1.0
    • Focus: programming logic and design concepts; develop foundational skills to apply logical thinking and create maintainable programs; emphasizes problem-solving, algorithm development, and systematic software design.
  • Learning outcomes (from Page 2)

    • Understand the disadvantages of unstructured spaghetti code
    • Describe the three basic structures: sequence, selection, and loop
    • Use a priming input to structure a program
    • Appreciate the need for structure; recognize structure and modularize unstructured logic
  • Core idea introduced (spaghetti code vs structured programming)

    • Professional programs can be very long and complex (airplane guidance, tax audits, payroll), containing hundreds or thousands of instructions.
    • Unstructured logic can become a “spaghetti code” mess: difficult to read, maintain, reuse, and build upon; prone to errors.
    • Structured programs follow rules that eliminate problems caused by spaghetti code.
  • Example and motivation: spaghetti code as an unpleasant, hard-to-follow flow (Figure 3-1 dog-washing flowchart)

    • Unstructured flow may still work, but readability and maintainability suffer, especially as complexity grows (e.g., washing many dogs concurrently, applying flea medication, grooming, genealogy research).
    • Goal: make unstructured processes clearer and less error-prone through structure.
  • Key example illustrating unstructured flow (dog-washing scenario)

    • A simple, short flow might be easy to follow if you know dog-washing steps, but becomes cumbersome with more steps or less familiarity.
    • The chapter promises to teach how to restructure such processes for clarity and reliability.
  • The three basic structures (Overview of Structure)

    • Any program can be constructed using only three structures:
    • Sequence
    • Selection
    • Loop
    • Each structure has a specific configuration diagrammed with flowchart symbols; the combination of these three can model any task.
  • The Sequence structure

    • Definition: actions/tasks executed in order, one after another.
    • Properties: any number of tasks; no branching; must continue step by step until the sequence ends.
    • Example: driving directions as a sequence:
    • go north on First Avenue for $3$ miles
    • turn left on Washington Boulevard
    • go west on Washington for $2$ miles
    • stop at $634$ Washington
    • Key point: no option to skip tasks within the sequence.
  • The Selection structure (Decision structure)

    • Purpose: choose one of two paths based on a condition (Boolean expression).
    • Boolean expression basics
    • A Boolean expression yields a true/false value (often represented as yes/no or 1/0).
    • Computer decisions yield true/false results; Boolean expressions control the selection structure.
    • Diagrammatic representation: starts with a decision symbol containing a Boolean expression; branches must join at the bottom.
    • Pseudocode representation: starts with if, ends with endif.
    • Example phrasing (dual-alternative ifs):
    • if someCondition is true then do oneProcess else do theOtherProcess endif
    • Examples: if traffic is backed up on Washington Boulevard then continue for $1$ block on First Avenue and turn left on Adams Lane else turn left on Washington Boulevard endif
    • Payroll example: if hoursWorked > 40 then regularPay + overtimePay else regularPay endif
    • Single-alternative selections (no else):
    • If it is raining then take an umbrella endif
    • If employee participates in dental plan then deduct $40 from employee gross pay endif
    • Note on terminology: dual-alternative ifs (two branches) vs. single-alternative ifs (possible null branch)
  • The Loop structure (While loop)

    • Definition: repeats actions while a tested condition remains true (loop body).
    • Flow: condition is evaluated, if true, execute loop body, re-evaluate condition, repeat until false.
    • Pseudocode: starts with while and ends with endwhile; the loop tests the condition before the first execution (pre-test loop).
    • Flowchart principle: begins with a decision symbol containing a Boolean expression; a branch returns to the evaluation.
    • Other note: there are post-test loops (test after the loop body) discussed later (Chapter 5); for this chapter, all loops are treated as while loops that test before execution.
    • Common terminology: iteration or repetition.
  • Understanding structure: Booleans and basic logic

    • Boolean expressions control decisions; true/false (or yes/no) results guide which path to take.
    • George Boole is recognized as the founder of mathematical logic; Boolean expressions are named after him.
  • Combining structures: stacking and nesting

    • All logic problems can be solved with the three structures, but they can be combined in infinitely many ways.
    • Stacking: attaching structures end-to-end (e.g., a sequence followed by a selection, then a loop).
    • Example: Figure 3-6 shows a sequence → selection → loop, with explicit pseudocode mapping:
    • StepA and StepB execute in sequence.
    • A selection starts with conditionC (Boolean expression).
    • If conditionC is true, execute StepD; else execute StepE; then outside the selection proceed with subsequent steps.
    • The loop starts with conditionF and continues while it remains true; inside the loop is StepG; the endwhile marks the end.
    • Nesting: placing a structure inside another structure (e.g., a sequence inside a selection, or a loop containing a nested selection).
    • Indentation is used to show blocks; there is no limit to nesting levels.
    • Important rule: when nesting, the start and end points of a structure line up on the same level; structures cannot overlap.
    • Example progressions (Figures 3-7, 3-8, 3-9): a sequence of three steps on one branch of a selection; a loop replacing a step; a nested combination (selection inside a loop, or loop inside a selection).
    • Visual alignment in pseudocode: if and endif align with the corresponding structure boundaries to reflect nesting levels.
  • Priming input and the end-of-data concept

    • Priming input: an initial input statement at the start of a structured loop to kick off the process.
    • Purpose: ensures the loop is entered in a structured way and the loop-controlling condition can be evaluated properly on the first iteration.
    • End-of-data condition and not eof?
    • Not eof? is a sentinel test used to determine when input ends.
    • A correct structured approach places the end-of-data test immediately after an input statement, to avoid extra or erroneous outputs after EOF is reached.
    • Number-doubling example (Figures 3-11, 3-14, 3-16):
    • Problem: doubling numbers until end-of-input; demonstrate structured vs unstructured looping.
    • Unstructured example (Figure 3-11): the flow ends up not returning to the loop test properly after the inner sequence; ends up looping erroneously.
    • Structured solution (Figure 3-16): includes a priming input (highlighted) and a loop that contains a sequence, with the last loop step capable of altering the EOF condition.
    • Priming input concept origin: “priming” because the first input gets the process going; the loop’s last action typically reads the next input value.
    • The last element inside the loop is often the input operation that alters the loop-controlling condition (e.g., not eof? test).
  • Structure: criteria and analysis of a structured program

    • A structured program:
    • Includes only the three basic structures (sequence, selection, loop).
    • Each structure has a single entry point and a single exit point.
    • Structures can be stacked or connected only at entry/exit points.
    • Any structure can be nested within another structure.
    • Recognizing structure can be tricky; examples and quick-reference visuals help determine whether a segment is structured (Figures 3-18 to 3-20 show some segments).
    • “Spaghetti bowl” method: a mental model to untangle an unstructured flowchart by progressively extracting a coherent sequence and then identifying subsequent selection/loop structures (Figure 3-20 steps 1–7).
  • The dog-washing example (from spaghetti to structured)

    • Part 1 (unstructured): Figure 3-1 shows the spaghetti code flow for dog washing; it can be read if you know the steps, but is hard to follow as logic grows.
    • Part 2: A systematic approach restructures the dog-washing process into a structured combination of sequences and loops (Figures 3-21, 3-22).
    • Part 3: Modularization (Figure 3-23): abstract repeated sequences into modules (e.g., catchDog, startWater) to shorten the main program and improve maintainability.
    • Key takeaway: any unstructured set of steps can be rewritten using only the three basic structures, with nesting and stacking as needed; modularization helps maintain and reuse code.
    • Practical design insight: replacing repeated blocks with module calls reduces duplication and future maintenance effort (e.g., adding temperature checks within a module affects all usages).
  • Why structure matters: benefits and professional practice

    • Reasons for using structure (Page 26):
    • Clarity: as programs grow, structure helps avoid confusion.
    • Professionalism: professional programmers and instructors expect structured programs.
    • Efficiency: modern languages support and encourage structured constructs; older languages can still be used in structured form.
    • Modularity: structured programs can be broken into modules that can be assigned to multiple programmers and reused across programs.
    • Modularity and maintenance (Page 27): structured programs are easier to modify, maintain, and extend; modules can be reused and updated in one place.
  • Recognizing structure and don’t-do-it guidance

    • Recognizing structure: some flowcharts appear nonstandard, but can still be structured by identifying sequence/selection/loop blocks and ensuring proper nesting.
    • Don’t-Do-It (unstructured loop example, Fig. 3-21): a loop whose body does not return to the controlling test immediately; violates structured programming rules.
    • Final message: any set of steps can be reduced to combinations of the three basic structures; with nesting and stacking, the logic can represent nearly any process.
  • Summary and quick reference ideas

    • Spaghetti code is unstructured; three basic structures enable clean, maintainable design.
    • Structure basics:
    • Sequence: actions in order; no branching.
    • Selection: one of two paths based on a condition; can be dual-alternative (if-then-else) or single-alternative (if … endif) with a null branch.
    • Loop: repeats while a condition remains true; pre-test loop convention here (while … endwhile).
    • Priming input is essential for proper structuring of loops that process multiple data items.
    • Structure supports clarity, professionalism, efficiency, and modularity; it also supports easier maintenance and reuse of code.
  • Two truths and a lie (conceptual check from the chapter)

    • Statement 1: Structured programs are clearer than unstructured programs. (True)
    • Statement 2: Structured programs are easier to modify and maintain. (True)
    • Statement 3: Structured programs are not easily divided into parts, making them less prone to error. (False – the opposite is true; structured programs are more easily modularized and less prone to error.)
  • Exercises and practical tasks (from the end of Lesson 3)

    • Exercise 1: In Figure 3-10, replicate the same logical structure as Figure 3-9 for a different real-world process and produce a flowchart or pseudocode.
    • Exercise 2: Redraw each given unstructured segment in Figure 3-24 to a structured form that preserves the same conditions.
    • Exercise 3: Write structured pseudocode for each example a–e from Exercise 2.
    • Exercise 4–10 (from pages 39–42): Various real-world processes to model structurally (robot movement, guessing a number, dictionary lookup, going from home to school, choosing a college, buying a shirt, etc.). Include at least two decisions and two loops where specified.
  • Notation and terminology recap

    • Structure: a basic unit of programming logic; three types:
    • Sequence
    • Selection
    • Loop
    • Entry/Exit: each structure has one entry and one exit point.
    • Nesting: structures within structures; indentation helps visualize the blocks.
    • Stacking: attaching structures end to end.
    • End tokens: if (endif), loop (endwhile), etc. used to mark the end of a control structure in pseudocode.
    • End-of-data sentinel: not eof? used to determine end of input; priming input helps initiate the loop safely.
  • References and additional context

    • Textual references to Figures and Quick Reference 3-1 illustrate the three basic structures and their combinations.
    • Final takeaway: Every structured program can be understood as a composition of sequences, selections, and loops, with proper nesting and modularization to support clarity and reuse.
  • Practical implications for exam prep

    • Be able to: identify whether a given flowchart or pseudocode is structured (and why or why not).
    • Convert unstructured flowcharts into structured equivalents by introducing sequences, selections, and loops where appropriate.
    • Demonstrate priming input usage in a loop to ensure proper data processing and end-of-data termination.
    • Explain advantages of structure: clarity, professionalism, efficiency, and modularity, including maintenance benefits.
  • Quick terms to remember

    • Spaghetti code: unstructured, hard to read/maintain
    • Structured programming: using only sequence, selection, and loop
    • Priming input: initial input to start a structured loop
    • Not eof?: sentinel test to detect end of input
    • Endif / Endwhile: pseudocode flow-control terminators
    • Nesting vs. Stacking: nesting = one structure inside another; stacking = attaching structures end-to-end
    • Modularity: breaking logic into reusable modules
  • Final note

    • The chapter emphasizes that structure improves readability, reliability, and reusability, and that any reasonable process can be described with the three basic structures, through careful nesting and modularization.