Documente Academic
Documente Profesional
Documente Cultură
z
2.1 C) CONTROL
STRUCTURE
Control Structure
2
Control Structure
What is Control
Structure?
3
Control Structure
Sequence
*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.
8
Example: Sequence Structure
Problem statement: Find the perimeter and
area of a rectangle.
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?
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
Selection structure:
• also called decision structure
17
Types of Selection
Selection
Structure
Types of Selection
If [Single 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.
18
19
Repetition
Control Structure
• 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
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.
Concept
Definition : A Sentinel-controlled /Looping condition specifies the
requirement for repeating the instructions depend on Sentinel
valus(condition to stop).
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