Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views25 pages

05 CS1 Unit 3 - Lesson 2

This document covers the concepts of unstructured spaghetti code and the three basic programming structures: sequence, selection, and loop. It explains how structured programming improves readability and maintainability, and provides examples of each structure. The document concludes with an assessment task requiring the creation of a flowchart and pseudocode using the three structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

05 CS1 Unit 3 - Lesson 2

This document covers the concepts of unstructured spaghetti code and the three basic programming structures: sequence, selection, and loop. It explains how structured programming improves readability and maintainability, and provides examples of each structure. The document concludes with an assessment task requiring the creation of a flowchart and pseudocode using the three structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CS 1 – Programming

Logic Formulation
Unit 3: Data Types, Structures, and Decisions
Lesson 2: Unstructured Spaghetti Code, Three Basic Structures
Learning Outcome

Determined the appropriate structure


when developing algorithmic solutions
to a problem.
Understanding
Unstructured Spaghetti
Code
Spaghetti Code
The popular name for logically twisted program
statements is spaghetti code, because the logic is as
hard to follow as one noodle through a plate of
spaghetti. Programs that use spaghetti code logic are
unstructured programs; that is, they do not follow
the rules of structured logic that you will learn in
this lesson. Structured programs do follow those
rules.
For example, suppose you start a job
as a dog washer, and you receive
instructions for how to wash a dog, as
shown on the left. This kind of
flowchart is an example of
unstructured spaghetti code. A
computer program that is structured
similarly might “work”—that is, it
might produce correct results—but
would be difficult to read and
maintain, and its logic would be
difficult to follow.
Understanding the
Three Basic Structures
Three Basic Structures
In the mid-1960s, mathematicians proved that any
program, no matter how complicated, can be constructed
using one or more of only three structures. A structure is
a basic unit of programming logic; each structure is one
of the following:
• sequence
• selection
• loop
Sequence Structure
In a sequence structure, we perform an action or task,
and then we perform the next action, in order. A
sequence can contain any number of tasks, but there is no
option to branch off and skip any of the tasks. (In other
words, a flowchart that describes a sequence structure
never contains a decision symbol, and pseudocode that
describes a sequence structure never contains an if or a
while.)
An example of a
Sequence Structure.
Once we start a series
of actions in a
sequence, we must
continue step by step
until the sequence
ends.
Selection Structure
In a selection structure or decision
structure, we ask a question and, depending on
the answer, we take one of two courses of
action. Then, no matter which path we follow,
we continue with the next task. (In other words,
a flowchart that describes a selection structure
must begin with a decision symbol, and the
branches of the decision must join at the
bottom of the structure. Pseudocode that
describes a selection structure must start with if
and end with endif.)
Selection Structure (Cont.)
Some people call the selection structure an
if-then-else because it fits the following statement:

if someCondition is true then


do oneProcess
else
do theOtherProcess
Double Alternative Selection Structure

Dual-alternative ifs (or dual-alternative


selections), contain two alternatives—the
action taken when the tested condition is true
and the action taken when it is false.
Single Alternative Selection Structure

•Single-alternative ifs (or single alternative


selections), in this structure, you do not take
any special action if the statement is false or the
answer to the question is no. The case in which
nothing is done is often called the null case.
Double Alternative Single Alternative
Selection Structure Selection Structure
In a loop structure, you
continue to repeat actions while
Loop Structure a condition remains true. The
action or actions that occur
within the loop are known as
the loop body. In the most
common type of loop, a
condition is evaluated; if the
answer is true, you execute the
loop body and evaluate the
condition again.
Loop Structure (Cont.)
In the loop structure, if the condition is still true, we
execute the loop body again and then reevaluate the original
condition. This continues until the condition becomes false,
and then we exit the structure. (In other words, a flowchart
that describes a loop structure always begins with a decision
symbol that has a branch that returns to a spot prior to the
decision. Pseudocode that describes a loop starts with while
and ends with endwhile.) We may hear programmers refer to
looping as repetition or iteration.
Some programmers call this
structure a while…do, or
more simply, a while loop,
because it fits the following
statement:

while testCondition continues to be true


do someProcess
Stacking Structures
All logic problems can be solved using only these
three structures—sequence, selection, and loop. The
three structures can be combined in an infinite
number of ways. For example, we can have a
sequence of tasks followed by a selection, or a loop
followed by a sequence. Attaching structures end to
end is called stacking structures.
Structured flowchart
and pseudocode with
three stacked structures
EXAMPLES
colourList = ["red","orange","yellow","green","blue","indigo","violet"]
PRINT ("The rainbow colours are: ")
FOR colour in colourList:
PRINT colour

LOOP/ITERATION
myAge = 12
PRINT "Next year I will be: "
PRINT myAge + 1
PRINT "In two years I will be: "
PRINT myAge + 2
PRINT "In three years I will be: "
PRINT myAge + 3

SEQUENCE
IF LENGTH( password ) < 8:
PRINT ("Your password is not valid!")

SELECTION
WHILE (timer > 0):
PRINT "Carry on playing..."
timer = timer - 1

LOOP/ITERATION
Assessment Task

1. Look for a problem which has a logical


solution that contains three (3) basic
structures and create a flowchart and
pseudocode of that logical solution.

(Pass your assignment in PDF file format and upload it in our


VLE. Deadline of this will be BEFORE FINAL EXAM)

You might also like