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

Control Structures

Uploaded by

farhan.segujja
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)
6 views

Control Structures

Uploaded by

farhan.segujja
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/ 26

1

CSC 1106
Introduction to Programming and
Problem Solving | Fundamentals of
Computer Programming

Control Structures

BSc CS|BIT
Lecture Outline

At the end of this lecture you will have understood:


◻ What control structure are.
◻ The different types of control structures.
◻ Develop problems using the decision logic structure in conjunction with the
sequential logic structure.
◻ Use problem-solving tools when developing a solution using the decision logic
structure.
◻ Use nested decision instructions to develop a problem solution.
◻ Distinguish the different uses of straight-through, positive, and negative nested
decision logic structures

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Control structures
◻ Control structures are programming components that analyze and choose
a direction in which a program flows based on certain parameters or
conditions.
◻ There are three basic types of logic, or flow of control, known as:
1. Sequence logic, or sequential flow

2. Selection logic, or conditional flow

3. Iteration logic, or repetitive flow

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Sequential Logic structures (Sequential Flow)

◻ Sequential logic as the name suggests follows a serial or sequential flow


in which the flow depends on the series of instructions given to the
computer.
◻ Unless new instructions are given, the statements are executed in the
obvious sequence.
◻ The sequences may be given, by means of numbered steps explicitly and
also, implicitly follows the order in which modules are written.
◻ The most commonly used and the simplest logic structure is the sequential
structure.
◻ All problems use the sequential structure, and most problems use it in
conjunction with one or more of the other logic structures.
It is the default flow of executing program instruction.
© IUIU – 2021. 9/23/24
◻ Walusimbi Hakim | walusimbi.hakim@iuiu.
Sequential Logic structures
◻ A programmer who uses the sequential logic structure is asking
the computer to process a set of instructions in sequence from
the top to the bottom of an algorithm
Start

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


The Selection Logic Structure

◻ The decision structure is one of the most powerful because it is


the only way that the computer can choose between two or more
sets of actions.
Without decisions, the computer would be nothing more than a fast
calculator.
◻ The decision logic structure uses the If/Then/Else instruction, it
tells the computer that If a condition is true, Then execute a set
of instructions, or Else execute another set of instructions.
◻ The Else part is optional, as there is not always a set of
instructions if the conditions are false.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


The Selection Logic Structure

◻ Notice the brackets and the indentation in the illustration of the


algorithm of the decision instruction.
◻ Programmers use these features when they are writing
instructions in the decision structure because they improve
readability.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


The Selection Logic Structure

◻ The True instructions are processed when the resultant of the


condition is True, and the False instructions are processed when
the resultant of the condition is False.
◻ A condition can be one of four things:
an expression using relational operators (greater than, less than, greater
than or equal to, less than or equal to, equal to, and not equal to);
a logical expression, that is, an expression that uses logical operators
(AND, OR, and NOT);
a variable of the logical data type (True or False);
or a combination of logical, relational, and mathematical operators.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Flow Chart of a selection Logic

◻ The figure shows a simple flowchart illustrating


the decision structure.
◻ In the figure, the resultant of the condition(s)
can be True or False depending on the data.
◻ From the decision block (the diamond), there
will be a flowline, a branch for each set of
instructions:
one branch for the instructions to be executed if the
resultant is True,
Another branch for the instructions to be executed if
the resultant is False.
◻ There can never be more than two flowlines
coming from a decision block since there are
only two possibilities for the resultant of the
condition(s).
© IUIU – 2021. 9/23/24
Walusimbi Hakim | walusimbi.hakim@iuiu.
Flow Chart of a selection Logic

◻ The True branch and the False branch can come from any of the
lower three points of the diamond.
◻ It is best to be as consistent as possible in choosing the points of the
diamond on which these branches are placed.
◻ The convention is to draw the True branch on the right and the False
branch on the left or bottom, although it depends on the decisions to
be made.
◻ The processing flow from both the True branch and the False branch
must connect to the rest of the processing somewhere before exiting
the program.
◻ There can be no dangling flowchart blocks—blocks with no flowlines
leading to another block, the processing must flow somewhere.
© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Single Condition

◻ A simple decision with only one condition and one action or set
of actions (one instruction or set of instructions) for True and
another for False is relatively simple.
◻ For example, 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.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Multiple Condition

◻ Decisions in which you have multiple conditions that lead to one


action or set of actions for True and another action or set of
actions for False are slightly more complicated than those with
single conditions.
◻ In these decisions you will use logical operators to connect the
conditions.
◻ As with decisions based on a single condition, the resultant will
determine whether the True or the False actions are taken.
◻ The decision structure becomes more complicated as the
number of conditions and/or the number of actions for a True or
False resultant increases.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Multiple If/Then/Else Instructions

◻ There are three types of decision logic you will use to write
algorithms for solutions consisting of more than one decision.
◻ These types of decision logic include
Straight through logic,
Positive logic, and
Negative logic.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Straight-through logic

◻ It means that all of the decisions are processed sequentially, one


after the other.
◻ There is no Else part of the instructions; the False branch
always goes to the next decision, and the True branch goes to
the next decision after the instructions for the True branch have
been processed.
◻ With decisions following straight-through logic, all conditions are
tested, to test a condition means to process a condition to get a
True or False resultant.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Straight-through logic

◻ Straight-through logic is the least efficient of all types of decision


logic because all the decisions must be processed.
◻ However, you must use it to solve certain problems—those that
require two or more unrelated decisions and those in which all
decisions must be processed.
◻ It is often used in conjunction with the other two logic types and in
data validation (checking data to make sure it is correct).
◻ It is also used with languages that have limited features in their
decision instruction.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Straight-through logic - Example

◻ The problem illustrated below is to find the amount to charge


people of varying ages for a concert ticket.
◻ When the person is under 16, the charge is 7000; when the
person is between the age of 16 and 64, the charge is 10000;
when the person is 65 or over, the charge is 5000.
◻ The conditions are the following:

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Straight-through logic - Example

◻ In the algorithm and flowchart in the Figure above, notice that


there is no Else in straight through logic.
◻ If the resultant of the condition is False, then the processing flow
drops to the next instruction.
◻ There is no need for the Else since there are no instructions for
the False branch.
◻ If the resultant of the condition is True, then the set of instructions
for the True part is processed, and the processing flow then drops
to the next instruction.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Straight-through logic - Example

◻ The set of conditions in the Figure above is more efficiently


executed by using either of the other types of logic since only one
condition has to be met in order to execute the correct set of
instructions.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Positive logic

◻ In a positive logic, not all of the instructions are processed, once


the resultant of a decision is True, it allows the flow of the
processing to jump out of the decision logic structure and
continue through the other sections of the program instead of
processing succeeding decisions,
◻ Whenever the resultant is False (the Else part of the decision),
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.

© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Negative logic

◻ This is similar to positive logic except that the flow of the


processing continues through the program when the resultant of
a decision is False.
◻ Whenever the resultant is True, another decision is processed
until the resultant is False, or there are no more decisions to
process.
◻ At that time, the True branch processes the remaining
instructions.
◻ Negative logic is the hardest to use and understand.
◻ Some series of decisions will require a combination of two or
more of these logic types.
© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Nested If instructions

◻ 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.
◻ You use the nested If/Then/Else structure only when one of several
possible actions or sets of actions are to be processed.
◻ In composing an If/Then/Else instruction, draw a large bracket around
each If/Then/Else in the algorithm to ensure that each instruction is in
the proper place.
◻ The top part of the bracket indicates the condition, the middle section
of the bracket indicates the True branch, or the Then part of the
decision.
◻ The bottom part of the bracket indicates the False branch, or the Else
part of the decision.
© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Nested if instructions - Example

◻ In the Figure above, the decision is If PayType = “Hourly” and says,


Is the value of the variable PayType equal to the string constant
“Hourly”?
◻ If the two are equal, then the resultant is True and the instruction set
for the True is processed.
◻ If the resultant is False, the instruction set for the False branch is
processed.
◻ The True branch uses a nested If/Then/Else instruction; the decision
is embedded in the True branch of the first decision.
◻ This decision says, are the hours worked greater than 40? If the
answer is “yes,” the resultant is True, and the pay is processed at the
overtime rate.
© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.


Nested if instructions - Example

◻ When the resultant is False, that is, when the hours are not greater
than 40, the pay is processed at the normal rate.
◻ When the resultant of the first decision is False, that is, when the
PayType is not equal to “Hourly,” then the pay is processed at the
salary rate.
◻ Notice that when the PayType is not equal to “Hourly,” the
embedded decision on the True branch cannot be processed—an
example of negative logic.
◻ A good programmer thoroughly tests algorithms that use decision
logic with many sets of test data.
◻ Every path in every algorithm is checked, and a separate set of data is
used for each path.
◻ It is important to test each path so errors will be eliminated.
© IUIU – 2021. 9/23/24

Walusimbi Hakim | walusimbi.hakim@iuiu.

You might also like