Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

(Week 7) Intro To Programming Structures II

This document provides an introduction to four logic structures used in programming: sequential, decision, case, and loop logic. It discusses each structure type and provides examples. For sequential logic, it explains that instructions are executed from top to bottom without conditions. For decision logic, it describes using if/then/else statements to choose between sets of instructions based on whether a condition is true or false. It also gives examples of decision logic with single and multiple conditions.

Uploaded by

chukwukaukandu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

(Week 7) Intro To Programming Structures II

This document provides an introduction to four logic structures used in programming: sequential, decision, case, and loop logic. It discusses each structure type and provides examples. For sequential logic, it explains that instructions are executed from top to bottom without conditions. For decision logic, it describes using if/then/else statements to choose between sets of instructions based on whether a condition is true or false. It also gives examples of decision logic with single and multiple conditions.

Uploaded by

chukwukaukandu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

www.covenantuniversity.edu.

ng

Introduction to Programming
structures II: The Four Logic
Structures:
Sequential logic, Decision logic, Case logic, Loop logic

Lecture Note (Week 7)


The CSC121 Team

1
2
Learning Objectives
 At the end of this lecture, you should be able to:
– Have mastered the art of Problem Solving using
algorithm, Flowchart and Pseudocode
– Use the Solution Development tools in Problem
Solving.
– Develop Solutions using the logic structure types
(Sequential, Decision, Case and Loop).

3
Lecture Outline
 Sequential Logic Structure
 Decision Logic Structure
 Case Logic Structure
 Loop Logic Structure

4
Sequential Logic Structure
 The Sequential logic structure is the simplest of
all logic structures.
 A programmer who writes the algorithm (set of
instructions) sequentially, wants the computer
to process the instruction from top to bottom.
 One instruction does not execute before the
previous one is executed.
5
Basic Flowchart symbols for the
Sequential Structure
 Start

 I/O

 Processing

 End/Stop/Exit
6
Sequential Logic Format
 Generally, it has the form:

ModuleName (list of parameters)


1. Instruction
2. Instruction
3….
XX.End, Exit, or Return (variable)
7
Sequential Logic Format

Note: The form of the flowchart must correspond to the form of the algorithm.
The instructions in the algorithm are numbered.
8
Example: The Interest Problem
Mary Smith is looking for the bank that will give the most return on her
money over the next five years. She has $2,000 to put into a savings
account. The standard equation to calculate principal plus interest at the
end of a period of time is
Amount = P * (1 + I/M)^(N * M)
Where:
– P= Principal (amount of money to invest, in this case $2,000)
– I= Interest (percentage rate the bank pays to the investor)
– N Number of Years (time for which the principal is invested)
– M = Compound Interval (the number of times per year the interest is
calculated and added to the principal)

9
Algorithm
InterestControl
1. Input Principal (P), Interest (I), Years (N), Time(M)
2. Calculate resulting amount after a given period
Amount = P * (1 + I/M)^(N * M)
3. Print Amount
4. End

10
Flowchart
Start

Input P, I, N, M

Amount = P * (1 + I/M)^(N * M)

Print
Amount

Stop
11 11
Pseudocode
Input Principal (P), Interest (I), Years (N), Time(M)
Calculate resulting amount after a given period:
Amount = P * (1 + I/M)^(N * M)
Print Amount
End

12
Lecture Outline
 Sequential Logic Structure
 Decision Logic Structure
 Case Logic
 Loop Logic

13
Decision Logic Structure
 The decision logic structure uses the
if/then/else instruction.
 The computer executes a set of instructions
if a condition is true or Else another set of
instructions will be executed.
 The Else part is optional
14
Decision Logic Structure

The decision logic structure Flowchart for the decision logic structure

15
Decision Logic Structure
 It is very powerful
 It allows the computer to choose between
two or more sets of actions.

16
Conditions in Decision Logic
 There are four(4) types of conditions:
– Logical expression that uses logical operators like
AND, OR, NOT, etc.
– An expression that uses relational operators like <=,
<, >, >=, etc.
– A variable of the logical data type (True or False)
– An expression with the combination of logical,
relational and mathematical operators.
17
Decision Logic with a single condition
 A decision logic with only one condition to
be fulfilled for the True part is relatively
simple.

18
Example 1
 If a decision is to be made in a program to
give two spoons of syrup to children not
more than 10 years old but give three spoons
to children more than 10 years old.

19
Pseudocode for Example 1
If age <= 10 Then From the pseudocode, we can identify
two numeric variables: age and
no_of_spoons = 2 no_of_spoons. If age is less than or equal
to 10, the value 2 is assigned to
Else no_of_spoons otherwise no_of_spoons is
no_of_spoons = 3 assigned the value 3.

Endif
The pseudocode above accomplishes the task on the previous slide using a decision logic

20
Flowchart for Example 1

21
Example 2
 Assume you are calculating pay at an hourly rate and
overtime pay (over 40 hours) at 1.5 times the hourly
rate.
 The decision to calculate pay would be stated in the
following way:
– If the hours are greater than 40,
• Then the pay is calculated for overtime, or
– Else
• the pay is calculated in the usual way

22
Algorithm, Flowchart & Pseudocode for
Example 2

23
Decision Logic with multiple conditions
 Decisions can also have multiple conditions to
be met before a/some set(s) of instructions can
be executed.
 In this case, such conditions are connected with
logical operators (AND, OR).
 The resultant Boolean value (True or False)
determines if the true part of the logic will
execute or not.
24
Decision Logic with multiple conditions
(2)
 Extending the Example 1, let us assume that adults
can also be given that syrup and therefore we want
adults more than fifty years of age to have two
spoons.
 In this new decision problem, we have two conditions
before two spoons of syrup can be given:
– A child must not be more than 10 years
– An adult must be more than 50 years

25
Pseudocode 2
1 2
If age <= 10 OR age > 50 Then 1 Condition 1

no_of_spoons = 2
Else
no_of_spoons = 3 2 Condition 2
Endif
Can the AND logical operator be used?

26
Multiple If/Then/Else Instructions
 A programmer may be faced with the problem
of making multiple decisions in a program,
three sub-categories of the decision logic
structure are employed in this scenario, they
include the following:
– Straight-through logic
– Positive logic
– Negative logic

27
Straight-Through Logic
 All the decisions are processed sequentially
 There is no else part
 When the condition is not met, then execution
goes to the next decision
 When the condition is met, the machine
executes the True branch of the logic before
moving on to another decision.
28
Straight-Through Logic (2)
 Often used when the conditions are
independent of each other
 It is the least efficient of all types of decision
logic because all decisions must be
processed
 An example is illustrated on the next slide
29
Example 3
 Given a problem where an amount is to be
charged on people of varying ages for a
concert ticket.
– When the person is under 16, the charge is $7; when
the person is 65 or over, the charge is $5; all others
are charged $10.
 Write a pseudocode to find the amount to
charge each age group.
30
Straight Through Logic Solution for
Example 3

Attempt drawing the


flowchart for this
pseudocode

31
Positive Logic
 Positive logic allows the flow of the processing to continue
through the module instead of processing succeeding
decisions, once the resultant of a decision is True.
 Whenever the resultant is False, another decision in the
sequence is processed until the resultant is True, or there
are no more decisions to process.
– At that time, the False branch processes the remaining instructions.
 It models the way we humans think and therefore easy to
understand
32
Positive Logic Solution for Example 3

33
Negative Logic
 Negative logic is similar to positive logic except
that the flow of the processing continues through
the module when the resultant of a decision is False
 It is the hardest logic for most people to
comprehend
 The positive side of this logic is that it can be used
to reduce the number of tests and also to improve
the readability of a program.
34
Negative Logic Solution for Example 3

35
Logic Conversion
 A programmer may want to transform a positive
logic to negative logic or vice-versa to improve
the efficiency or readability of a solution.
 In a decision, there must always be instructions
for a True section, but not always for a False
section.
– If there are no instructions for the True section of a
decision instruction, then it is better to convert the
logic type.
36
Logic Conversion (2)
 To perform logic type conversion, do the following:
– Change all < to >=
– Change all <= to >
– Change all > to <=
– Change all >= to <
– Change all = to <>
– Change all <> to =
– Interchange all of the Then set of instructions with the
corresponding Else set of instructions.

37
Choosing a decision logic
 The following questions should be answered before
a selecting a particular decision logic:
– Which type would make the solution most readable?
– Which type would make the solution the easiest to
maintain or change?
– Which would require the fewest tests when you don’t
know anything about the data?
– Which would require the fewest tests when you’re given
some data?

38
Nested If/Then/Else Statement
 It should be noted that only the positive and
negative logic decisions use nested
if/Then/Else statements and not straight-through
logic decisions.
 Nested If/Then/Else instructions are sets of
instructions in which each level of a decision is
embedded in a level before it; like nesting
baskets, one goes inside the next
39
Nested vs. Non-Nested If/Then/Else
Logic

Non-nested If/Then Nested If/Then

40
Lecture Outline
 Sequential Logic Structure
 Decision Logic Structure
 Case Logic
 Loop Logic

41
Case Logic Structure
 The case logic structure is used when one
needs to select one action from a set of
actions.
 It performs almost the same action as the
decision logic structure only that it has a
different structure of its own.

42
Format of a case logic structure
CaseOf
<condition1>
Actions for <condition1>
<condition2>
Actions for <condition2>

Otherwise:
actions when all other conditions are False
EndOfCase

43
Case Logic Structure Format

44
Case Logic Structure Example
 Consider a problem where each student in Covenant
University has a bank account.
 The lecturing team of CSC121 decides to pay the
amount of ₦2,000.00 times the weight (unit) given to
a program to the account of each student in the class.
 Write a case logic in form of an algorithm that will
solve this problem such that students not offering the
course are not reckoned with.
45
Solution Process
 The first step to solving the problem is identifying the
students offering CSC121.
 The students we are concerned with are in the following
programmes:
– Computer Science, MIS: Weight = 2
– Industrial Physics, Industrial Chemistry: Weight = 2
– Biochemistry, Microbiology: Weight = 1.5
– Applied Biology and Biotechnology: Weight = 1.5
– Industrial Mathematics: Weight = 1.0

46
Solution Process (2)
 To determine if a student will receive the
amount, we check if s/he is in any of these
programmes.
 We set up the following variables:
– prog: stores the programme of the concerned
student
– amount: stores the amount to be deposited into
student’s account

47
Solution
CaseOf
Prog = “Computer Science”
amount = amount + (2000 * 2)
Prog = “MIS”
amount = amount + (2000 * 2)
Prog = “Industrial Physics”
amount = amount + (2000 * 2)
Prog = “Industrial Chemistry”
amount = amount + (2000 * 2)
Prog = “Biochemistry”
amount = amount + (2000 * 1.5)
48
Solution
Prog = “Microbiology”
amount = amount + (2000 * 1.5)
Prog = “Applied Biology and Biotechnology”
amount = amount + (2000 * 1.5)
Prog = “Industrial Mathematics”
amount = amount + (2000 * 1.0)
Otherwise:
amount = amount + 0.00
EndOfCase
49
Lecture Outline
 Sequential Logic Structure
 Decision Logic Structure
 Case Logic
 Loop Logic

50
Loop Logic Structure
 Is usually for repetitive tasks.
 Example: Arranging the names of CSC 121 students
alphabetically.
 Looping tasks include counting (incrementally or
decrementally), accumulating/adding and
Multiplying.
 In a looping task, a number is added or subtracted
from a variable, and the result is then stored back
into the same variable.
51
Loop Logic Structure

52
Looping Task: Incrementing
 Also known as counting.
 The value of a variable is increased by
adding a constant.
 Example:
Counter = Counter +1; //incrementing
Counter = Counter – 1; //decrementing
53
Looping Task: Accumulating
 Is usually used for summing up.
 It is initialized to 0
 Instead of adding a constant to the value of a
variable, you add a variable.
 Example:
– Sum = Sum + Variable
54
Looping Task: Product
 Similar to Sum
 Initialized to 1
 Example:
Product = 1
Product = Product * Number

55
Types of Loop Logic Structure
 There are three(3) loop types:
– While/WhileEnd
– Repeat/Until
– Automatic counter

56
Types of Loop Logic Structure:
While/WhileEnd
 It repeats a set of instructions while a condition is
True and stops when False.

57
Types of Loop Logic Structure: Algorithm
and Flowchart for While/WhileEnd

58
Types of Loop Logic Structure:
Repeat/Until
 It repeats instructions while a condition is
False and stops when it is True.
 The condition is processed at the end.

59
Types of Loop Logic Structure:
Repeat/Until

60
Types of Loop Logic Structure: Automatic
Counter
 The initial variable value is increased by an
equal increment till it is greater than the
ending number.
 The beginning value, the ending value, and
the increment value may be constants,
variables, or expressions (calculations).

61
Types of Loop Logic Structure: Automatic
Counter

62
Assignment 1:
 Design and write the pseudo code, flowchart
and C program to find the average age of all
the students in a class using at least (2)
Looping techniques.
– Solution should be written inside your lecture
Note.

63
Assignment 2:
 Give a minimum of 3 pages summary on
Chapter 5,6,7 of the Problem Solving
Techniques and Programming Concept by
Maureen Sprankle and Jim Hubbard.
– Lifeline.

64

You might also like