0% found this document useful (0 votes)
2 views

IPRG5111_Lecture 5S_2024

The document discusses programming logic, focusing on structured programming and its advantages over unstructured 'spaghetti code.' It outlines the three basic structures of programming: sequence, selection, and loop, and emphasizes the importance of clarity, professionalism, and maintainability in code. Additionally, it includes practical exercises for students to apply their understanding of these concepts through flowcharts and pseudocode.

Uploaded by

shantonjohnson98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IPRG5111_Lecture 5S_2024

The document discusses programming logic, focusing on structured programming and its advantages over unstructured 'spaghetti code.' It outlines the three basic structures of programming: sequence, selection, and loop, and emphasizes the importance of clarity, professionalism, and maintainability in code. Additionally, it includes practical exercises for students to apply their understanding of these concepts through flowcharts and pseudocode.

Uploaded by

shantonjohnson98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

(http://valenciacollege.edu/asdegrees/information-technology/images/it-computer-programming-analysis.

png)

HMAW – IPRG5111
Introduction to Programming Logic
Lecture 5
Objectives
• Chapter 3: Lecture 4
• Spaghetti code
• Working with logical structures

• Chapter 4
• Sequential Logic
Lecture 5
• Group Activity
• Practical
Chapter 3 - Logic Structures
The disadvantages of unstructured spaghetti code
Spaghetti code is a term used for any source code that is hard to
understand. It has no defined structure.
The Disadvantages of Unstructured
Spaghetti Code
• Spaghetti code
– Logically incorrect (snarled) program statements
– Often a complicated mess
– Programs often work but are difficult to read and maintain
– Confusing and prone to error
• Unstructured programs
– Do not follow the rules of structured logic
• Structured programs
– Follow the rules of structured logic
Basic prevention steps that could and should be taken:
• Comments
Comments will not only help the programmer, but also others that are reading
through the code. Provides clarity where needed.

• Understanding the Codebase


When a programmer joins an existing team, it would be good to learn their methods
before editing significant areas of coding in software applications.

• Perform Unit Tests


Performing routine unit tests will help maintain code and reduce the probability
of spaghetti code.

• Use Light Frameworks


There are many new frameworks that can be built and used that helps to execute
functions in a few lines of code, making your code leaner and helping to fix bugs.

• Always Double Check


Always go through your code one more time. Revisiting what you have coded, will
validate your source code and its purpose.
Understanding the Three Basic Structures
Understanding the Three Basic Structures
The Sequence Structure

- Perform actions or tasks in order


- No branching or skipping any task
The Selection Structure
The Selection Structure
• Dual-alternative ifs
– Contains two alternatives
– The if-then-else structure

if someCondition is true then


do oneProcess
else
do theOtherProcess
endif
The Selection Structure

• Single-alternative ifs

if employee belongs to dentalPlan then


deduct $40 from employeeGrossPay

– An else clause is not required


• null case
– Situation where nothing is done
The Selection Structure
The Selection Structure
The Loop Structure
• Loop structure
– Repeats a set of actions while a condition remains true
• Loop body
– Also called repetition or iteration
– Condition is tested first in the most common form of loop
– The while…do or while loop
The Loop Structure
The Loop Structure
• Loop structure
while testCondition continues to be true
do someProcess
endwhile
while you continue to be hungry
take another bite of food
determine if you still feel hungry
endwhile
The Three Basic Structures -
Overview
• Sequence structure
– Perform actions or tasks in order
– No branching or skipping any task
• Selection structure (decision structure)
– Ask a question, take one of two actions based on testing a
condition. Known as evaluating a Boolean expression, a
statement that is either true or false
– Often called if-then-else
– Dual-alternative ifs or single-alternative ifs
• Loop structure
– Repeat actions while a condition remains true
Combining Structures
• All logic problems can be solved using only sequence,
selection, and loop
• Structures can be combined in an infinite number of
ways
• Stacking structures
– Attaching structures end-to-end
• End-structure statement
– Indicates the end of a structure
– The endif statement ends an if-then-else structure
– The endwhile statement ends a loop structure
Combining Structures
Combining Structures
• Nesting structures
– Placing one structure within another
– Indent the nested structure’s statements
Combining Structures
Combining Structures
Combining Structures
Combining Structures
Combining Structures
• Structured programs have the following characteristics:

– Include only combinations of the three basic structures


– Each structure has a single-entry point and a single exit point
– Structures can be stacked or connected to one another only at
their entry or exit points
– Any structure can be nested within another structure
Understanding the Reasons for Structure
• Use structured programming for:
– Clarity: unstructured programs are confusing
– Professionalism: other programmers expect it
– Efficiency: most languages support it
– Maintenance: other programmers find it easier to read
– Modularity: easily broken down into modules

• Structured programming is sometimes called goto-


less programming
Recognizing Structure
Chapter 4 – Sequential Logic
The most common and simplest logical structure is the sequential
logic structure. All our programming solutions use sequential logic.
The program processes instructions one after the next in the order
in which it has been written.
Chapter 4 – Sequential Logic
Chapter 4 – Sequential Logic
Group Exercise - Working in groups of 2 or 3 (no more than 3)
STEP 1:
Identify a problem that you and your group member(s) would like to design a solution for and
provide a summary describing the problem. This section needs to contain a description of the
problem area you are trying to address. It should encompass all aspects of the problem.
You are to create the following in your group:
•Problem Definition (part 1)

STEP 2:
Detail the actual design elements of the program, namely: program flow (how the program
works).
You are to create the following in your group:
•IPO table (part 2)
•A flowchart (part 3)
•Pseudocode (part 4)

Your design must make use of the following elements/structures:


-Variables and constants
-Input, processing and output
-Try and include a selection structure.
Example of Problem Statement -
IPO table – Flowchart –
Pseudocode
Exercise 1
1. Draw a flowchart and write pseudocode to represent the logic of a
program that allows the user to enter a value. The program divides
the value by 2 and outputs the result.

start
output “Please enter in a number“
input myNumber
set myAnswer = myNumber / 2
output myAnswer
stop
Exercise 2
2. Draw a flowchart and write pseudocode to represent the logic of a
program that allows the user to enter two values. The program outputs
the product of the two values.
3. Draw a flowchart or
Exercise 3
& write pseudocode to represent the logic of a program
that allows the user to enter a value for hours worked in a day. The program
calculates the hours worked in a five-day week and the hours worked in a 252-
day work year. The program outputs all the results.

Pseudocode
start
input hoursWorkedDay
set hoursWorkedWeek = hoursWorkedDay * 5
set hoursWorkedYear = hoursWorkedDay * 25
output hoursWorkedWeek
output hoursWorkedYear
stop
Exercise 4
&
Working with Variables
Example:
Example:
Example:
The current 3-character prefixes
that will be used with the names
of variables. This could serve as
an extra classification of the
variable.

The prefix will always be written


in small characters, immediately
followed by a capital letter for
the chosen name.
And create a flowchart.
start
Declaration //declaring each variable with the appropriate data type
num fltTestMark1
num fltTestMark2
num fltAssignmentMark
num fltSemesterMark

output “Please enter your first test mark”


input fltTestMark1
output “Please enter your second test mark”
input fltTestMark2
output “Please enter your assignment mark”
Input fltAssignmentMark

set fltSemesterMark = (fltTestMark1*0.15) + (fltTestMark2*0.15) + (fltAssignmentMark*0.2)

output fltSemesterMark
stop
Practical's
STEP 1: Create a folder on your drive
STEP 2: OPEN NOTEPAD
Practical's
STEP 1: Create a folder on your drive
STEP 2: OPEN NOTEPAD

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index - [Name of Program]</title>
</head>
<body>
<h1>Index - [Name of the program]</h1>
<h4>
The following webpage will execute the script file named
"code1".
</h4>
<script src="js/code1.js"></script>
</body>
</html>
Practical's
STEP 3: Type the code below in NOTEPAD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index - [Name of Program]</title>
</head>
<body>
<h1>Index - [Name of the program]</h1>
<h4>
The following webpage will execute the script file named
"code1".
</h4>
<script src="js/code1.js"></script>
</body>
</html>
Practical's
STEP 3: Saving the doc
Practical's
STEP 3: Saving the doc
Practical's
STEP 4: Open a NEW NOTEPAD and type the following

STEP 5: SAVE as the following:


Practical's
STEP 6: Check that you have the following:

STEP 7: Double click on test code to open


Practical's
STEP 8: Right click (anywhere on the site) and select “inspect”
Practical's
STEP 9: You should be looking at the following:
Practical's
STEP 10: Copy the path of code1_1 by right clicking and selecting “copy as path”
Practical's
STEP 11: Right click on the testcode file and select edit in Notepad
Practical's
STEP 12: Edit the code, see below:

Before
Practical's
STEP 12: Paste the link, see below:

After
Practical's
STEP 12: Save and close testcode and then double click on the file testcode
Practical's
STEP 12: Save and close testcode and then double click on the file testcode

You might also like