Sunteți pe pagina 1din 24

DESIGN A SOLUTION

z
2.1 C) CONTROL
STRUCTURE
Control Structure

All computer programs, no matter how simple or


how complex, are written using one or more of
three basic structures; sequence, selection, and
repetition.

2
Control Structure
What is Control
Structure?

A pattern for controlling the flow of logic in a


computer program

The three fundamental control structures are:


 Sequence
 Selection
 Looping

3
Control Structure
Sequence

The sequence structure directs the


computer to process the program
instructions, one after another, in the
order in which they are listed.

*You will use the sequence structure in every program you write.

4
Sequence Structure
Sequence

Pseudocode Flowchart
Start
Start
statement_1
statement_2
statement_3 statement_1
.
. statement_2
.
statement_n statement_3
End
statement_n

End
5
Control Structure
 What is a Control Structure?
• Control structure is use to control the logic of the
data flow through the program design.

• The three basic control structures are:


• Sequence or Sequential
• Selection or Decision
• Looping
6
Control Structure: Sequence
 The sequence or sequential structure
executes instructions one after another in a
sequence, and processes data one instruction
after another.
 This logic structure is the most widely used,
and it is normally used in conjunction with
others (selection and looping).
7
Algorithm Flow Chart Pseudocode
Sequence Structure
.
. .
. .
.
Instruction Instruction
5. Instruction Instruction
6. Instruction Instruction
7. Instruction Instruction .
. .
. .
. Instruction

8
Example: Sequence Structure
 Problem statement: Find the perimeter and
area of a rectangle.

 The perimeter and area of the rectangle are


then given by the following formulas:

perimeter = 2 * (length + width)


area = length * width
9
1. Analyze the problem:
• Problem Analysis Chart (PAC)
• Input Process Output Chart (IPO)
PAC:
Data Processing Output
length perimeter = 2 * (length + width) perimeter
width area = length * width area

Input Processing Output


length 1. Read length and width perimeter
width 2. Calculate perimeter area
perimeter = 2 * (length + width)
3. Calculate area
area = length * width
4. Print perimeter and area
10
2. Design an algorithm:
• Pseudocode

Pseudocode
Must Start
begin with Read length, width
Calculate perimeter
perimeter = 2 * (length + width)
Calculate area
area = length * width
Print perimeter, area
Must end End
with

11
Start
2. Design an algorithm:
• Flow Chart
Read length,
Pseudocode width

Start
Read length, width
perimeter = 2 * (length + width)
Calculate perimeter
perimeter = 2 * (length + = area = length * width
width)
Calculate area
area = length * width Print perimeter, area
Print perimeter, area
End
End
12
8.2.3.2 Control What is Control
Structure
Structure?

A pattern for controlling the flow of logic in a


computer program

The three fundamental control structures are:


 Sequence
 Selection
 Looping

13
Control Structure

Sequence
The sequence structure
directs the
computer to process the program
instructions, one after another, in
the order in which they are listed.

*You will use the sequence structure in every program you write.

14
Sequence Structure
Sequence

Pseudocode Flowchart
Start
statement_1 Start
statement_2
statement_3
. statement_1
.
statement_2
.
statement_n statement_3
End
statement_n

End
15
16
Control Structure
Selection

The selection structure directs the computer to make a


decision (based on some condition), and then take an
appropriate action based upon that decision.

Selection structure:
• also called decision structure
17
Types of Selection
Selection
Structure

Types of Selection

If [Single alternative]

if-else [Dual alternative]

Multiple Alternatives

Nested Selection
Control Structure Single Alternative Selection /
Selection if statement

Concept
The single alternative selection / if statement requires
one or more actions to be taken only when its condition
evaluates to true.

Examples of single alternative selection structures :


*if (its raining), take an umbrella
*if (its night), turn your headlights on

18
19
Repetition
Control Structure

Programmers use the repetition structure, when they


need the computer to repeatedly process one or more
program instructions

• Repetition structure allow to repeat one or more instructions until some conditions
is met.
• The number of repetitions depends on when the expression turns false
• Repetition enables a program to perform one or more actions repeatedly as long as
a certain condition is met

Repetition structure:
• also called looping control structure
20
Repetition
Control Structure

Counter Controlled Counter

(when we know how Accumulator


many times loop

2 ways to stop loop


body will be Both Counter &
executed ) Accumulator

Repetition
Sentinel Controlled
(when we don’t know
exactly know how many
times loop body will be
executed

Repetition structure:
• also called looping control structure
21
Repetition Counter-Controlled
# a counter is a numeric variable used for
counting something.

Concept
Definition : A counter-controlled /Looping condition specifies the requirement for
repeating the instructions.
Indicates when the computer should continue “looping” through the instructions.

The counter-controlled has 4 important parts:


(1) Name the counter : any names can be given to the loop counter
(2) Initialize the counter :
* initializing means to assign a beginning value to the counter .
Counters are usually initialized to the number 0 or 1; however they can be
initialized to any number; depending on the value required by the algorithm.
(3) Condition to test the final value of loop counter.
(4) Update the counter (increment/decrement) :
* means adding a number to the value stored in the counter. The number can be either
positive or negative, integer or non integer.
example
start
counter =1 (i)
total = 0
while (counter ≤ 10 )  ©
read number
total = total + number
counter = counter +1 (u)
end while
print total
end
23
Repetition Sentinel-Controlled
# Loops will stop depends the condition

Concept
Definition : A Sentinel-controlled /Looping condition specifies the
requirement for repeating the instructions depend on Sentinel
valus(condition to stop).

The Sentinel-controlled has 4 important parts:


(1) Name the sentinel values : any names can be given to the
loop (2)Initialize/priming read :
Initialize the loop condition by providing the first values
(3) Update the counter (increment/decrement) :
update the values of the input item
(4) . Condition to test sentinel values.
example

start
read weight, height (i)
while (weight > 0 and height >0) ©
BMI= weight / (height x height)
print BMI
read weight, height (u)
end while
end

S-ar putea să vă placă și