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

Control Structures: Creative Technology 3

1. The document discusses different types of control structures used in programming including sequence, selection, and repetition. 2. Selection structures like if/else and switch statements allow a program to make decisions and choose between multiple paths. Repetition structures like while and for loops allow code to repeat until a condition is met. 3. Control structures are important building blocks that enable programs to bifurcate, repeat code, or bypass sections based on conditions, directing the flow of instructions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views

Control Structures: Creative Technology 3

1. The document discusses different types of control structures used in programming including sequence, selection, and repetition. 2. Selection structures like if/else and switch statements allow a program to make decisions and choose between multiple paths. Repetition structures like while and for loops allow code to repeat until a condition is met. 3. Control structures are important building blocks that enable programs to bifurcate, repeat code, or bypass sections based on conditions, directing the flow of instructions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SELF-LEARNING PACKAGE IN

Creative Technology 3

9SPSTE Quarter 3 | Week 2

Control Structures
Learning Competency:
Describe how control structures is used in programming.
SSP_TLE-CT8AP-IId-m-3.1

Page Quarter 2 | Week 3


Ready to Launch!
A program is a sequence of instructions. In the ordinary "flow of control", the computer executes the instruc-
tions in the sequence in which they appear, one after the other.
In this lesson, we will discuss how program instruction works, how do we read program code line by line (form
top to bottom, and from left to right). At some point, the program may reach a situation where it needs to make a decision
such as jump to a different part of the program or re-run a certain piece again. These decisions that affect the flow of the
program’s code are known as a Control Structures.

Aim at the Target!


At the end of this module you are expected to:
1. Identify each of the control structures.
2. Explain the flow of instructions for each of the control structures.

Try This!
Direction. Unscramble the following words with the help of the given clue.
WORD CLUE
POLO The purpose is to repeat a statement a certain number of times or while a condition
is fulfilled.
TCEELSNIO It is used to execute one or more statements if a condition is met.

UENEESQC Set of ordered instructions executed by a computer one after another.

OOLRTCL ERUTCURTS Commands that enable a program to take decisions, following one path or another

ROF POLO Allows a statement to be executed a specified a number of times.

Keep This in Mind!


As mentioned earlier, we read program instructions line by line starting from top to bottom, and from left to
right. This is the same way the ordered instructions are executed by a computer. An example of it can be seen in figures
A and B below.
Activity 1. Reading program instruction

Figure A Figure B

Page 2 Quarter 2 | Week 3


Analysis:
1. Enumerate how you read the program instructions (from top to bottom) in figure A.
2. Enumerate how you read the program instructions (from top to bottom and left to right ) in figure B.

Abstraction and Generalization


What is control structure?

Control Structure can be considered as the building blocks of computer programs. They are commands
that enable a program to “take decisions”, following one path or another. A program is usually not limited to a
linear sequence of instructions since during its process it may bifurcate, repeat code or bypass sections. Control
Structures are the blocks that analyze variables and choose directions in which to go based on given parame-
ters.

Type of Control Structure

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

Example:

Supposed we want to compute for the sum of two numbers. In order to


do that, we need to get the values of the two number, add the numbers,
and display its sum. The flowchart below consists of different steps follow-
ing the sequence of instructions in order to get the sum of the two num-
bers.

Fig. 1 Sequence structure

Fig. 2 Sequence Example Flowchart:

Page 3 Quarter 2 | Week 3


2. Selection
The Selection structure allows the computer to decide between two or more different courses of action by
testing conditions that occur as the program is running.
It also called decision structure.
There are two types of Selection structures:

A. Binary selection. There are two possible choices to choose. Binary uses IF… ELSE... ENDIF. If the condition is met(true)
then one path is taken, otherwise (false) the other path is taken.

Fig. 3 Binary Selection structure

Example:

Supposed you are task to compute for the


salary of employees. The computation will be based
on their hours worked. A condition is set to deter-
mine whether it will compute for the regular pay or
overtime pay.
The solution is illustrated in the example
flowchart. Here, we can see that the path to be cho-
sen will depend on the input of the employees num-
ber of hours worked. So, the action taken is de-
pendent on the number of hours spent working. If
greater than 40, then overtime pay is computed if
not, it will compute for the regular pay. The last task
is to print the paycheck of the employee.

Fig. 4 Binary Selection Example Flowchart:

Page 4 Quarter 2 | Week 3


B. Switch Case selection
• Case selection is where there is more than one possible choices to choose when trying to solve the prob-
lem. Only one process can be carried out.
• The condition is checked and if the first choice is true then it is carried out. If the choice is false , then the
second will be checked. If it is true then it is carried out. If no choice is found to be true then the other-
wise choice will be carried out.

Switch statement actually gathers many statements


of several cases.
- the case labels denote the specific statement
from which the execution of this group statements
begins.
- all statements till the end of the group are exe-
cuted sequentially.
To separate the cases, break statement is used.

- break breaks the sequential execution of the


statements and immediately jumps to the end of the
switch statement.
Default case is optional. It has no associated val-
ue. If the default case is present, control will transfer
to it if no other case value matched.
If there is no default case, and no other vale matches,
control falls through to the statement after the switch

Fig. 5 Switch Case structure

Page 5 Quarter 2 | Week 3


Fig. 6 Switch case Example Flowchart

In this example, The value in the variable age is compared to the first “case”, which is the value 18 (also
called the listed value) using an equality comparison or is “age equal to 18”. If it is true, the message is assigned
the value “You can vote.” and the next line of code (the break) is done (which jumps us to the end of the con-
trol structure). If it is false, it moves on to the next case for comparison. The last item is referred to as the de-
fault. If the age is not equal to 18, 40 or 60 you get the default message “ Error”.

3. Repetition
The Repetition structure directs the computer to repeat one or more instructions until some condition is
met.
• It is also called loop or iteration.
• The loop is created to return the program to where the repetition has started as long as it takes until
the condition is met.
Types of loop:
1. While
2. Do-while
3. For loop

Fig. 8 Do-While loop structure


Fig. 7 While loop structure
Page 6 Quarter 2 | Week 3
While and Do-While Loop Comparison

While Loop Do-While Loop


The while loop evaluations the condition first The do-while loop executes the statements first before
and then execute the statements. evaluating the condition.
The condition is specified at the beginning of The condition isn’t specified until after the body of the
the loop. loop.
The body is executed only if a certain condi- The body is always executed at least once, regardless of
tion is met and it terminates when the condi- whether the condition is met.
tion is false.

In this example, supposed we initialized the values of i


and n;

i = 0
n = 5
• When we perform the this statement , the value of i is
now equals to 1 (0 + 1 = 1).

• When this condition (1 <= 5) is tested the result is true.

• So the control flow of instruction goes back to the state-


ment and perform again the operation 1 + 1, and so
the value of i now is 2.

• The condition (2 <= 5) is tested again, giving the


result of True
Fig 9 Do-While loop structure Example
• The control flow of instruction goes back again to the
Flowchart
statement and perform again the operation 2 + 1,
and so the value of i now is 3.

So the loop goes on and on until the condition becomes false,


that is when i is equals to 6.

If we take the same sample initialization values of i and n in


the above example, the same thing will happen using While
loop, except that in the flow of instruction it will begin by the
testing the condition first then followed by executing the
statement.

Fig 10 While loop structure Example


Flowchart
Page 7 Quarter 2 | Week 3
For Loop allows a statement to be executed a specified a number of
times.

The for loop begins with a loop control variable


assigned a specific initial value. This control
variable in then incremented( or decremented) by
a specified amount each time around the loop until
a specified terminating value is reached at which
time the statement following the loop is then exe-
cuted.

Fig. 11 For Loop Structure

In the example, counter is the control variable


and counter = 1 is the first statement which
initializes counter to 1.

The second expression, counter <= 5 is the


loop continuation condition which repeats the
loop till the value of counter exceeds 5.

Finally, the third expression,


counter = counter + 1 increases the
value of counter by 1.

So the above piece of code will print 'hello' five


times.

Fig 12 For loop structure Example Flowchart

Page 8 Quarter 2 | Week 3


Application.
Direction. Identify the name of each control structures as shown by the following figures below.
Activity 2. Naming a Control Structure
Figure C

Figure A Figure B

Figure E Figure F
,

Figure D

Reflect
After you performed the activity above,
1. Did you find difficulty in identifying the different control structures? Why or why not?
2. How does this activity help you understand the flow of instructions of each control structure?

Reinforcement & Enrichment


Direction. Answer the question below.
Explain the flow of instructions as shown in the
flowchart.

Page 9 Quarter 2 | Week 3


Assess Your Learning
I. Multiple Choice. Select the letter of the correct answer.
Figure A.
1. What type of loop was used in Figure A?
A. Do While loop b. While loop c. For loop d. Case
A 2. What is the value of i during the first loop in figure A?
a. 8 b. 5 c. 2 d. none of the above
3.What is the last value of i that returns the statement true in Fig-
ure A?
a. 9 b. 5 c. 8 d. none of the above
4. What type of Selection structure was used in Figure B?
C a. Sequence b. Repetition c. Switch d. Loop
5. Which one of the following is the CORRECT variable in figure B?
B a. Orange b. Peach c. Fruit d. Apple
6. How many cases are there in figure B?
D A. 4 b. 5 c. 6 d. none of the above

For numbers 7-10, identify the parts of the loop structure labeled
A, B, C, D in Figure A.

Figure B.

References & Photo Credits


https://towardsdatascience.com/essential-programming-control-structures
https://www.academia.edu/24372685/CHAPTER_4_CONTROL_STRUCTURES_I_SELECTION
https://www.scribd.com/document/437581359/Control-Structure-and-Method
https://www.slideshare.net/AniLK0221/control-structure-c
https://towardsdatascience.com/essential-programming-control-structures-2e5e73285df4
https://www.tpub.com/progintro/1a.htm
https://slideplayer.com/slide/8229951/
https://whyisdifference.com/technology/difference-between-while-and-do-while-loop.html
https://beginnersbook.com/2017/08/cpp-for-loop/
https://www.tenouk.com/clabworksheet/labworksheet7_1.html
https://www.geeksforgeeks.org/nested-switch-case/
https://otrasteel.blogspot.com/2018/01/35-cannot-jump-from-switch-statement-to.html
https://www.slideshare.net/chaihidalgo/switch-case-and-looping-14700449
https://www.oreilly.com/library/view/computer-science-programming/9781449356835/fivedot2_while_loops.html
http://www.trytoprogram.com/c-programming/c-programming-for-loop/
https://www.rff.com/structured_flowchart.php
https://www.slideshare.net/jinmike008/switch-statements
https://press.rebus.community/programmingfundamentals/chapter/case-control-structure/
FLOWCHART SYMBOLS. https://www.pinterest.ph/pin/470415123569086615/visual-search/?x=14&y=13&w=453&h=426

Prepared by: MARICAR R. PORNEL, Teacher 1


Page 10 Quarter 2 | Week 3
GLOSSARY:

FLOWCHART . It is a type of diagram that represents an algorithm, workflow or process. The flowchart shows
the steps as boxes of various kinds, and their order by connecting the boxes with arrows. This diagrammatic
representation illustrates a solution model to a given problem.

Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.

Basic symbols of Flowchart

Note. These are the flowchart symbols used in our examples.

Building blocks . Building blocks may refer to the components of a particular programming language. In
Java, one example component of a building blocks are statements. Statements are action or sequence of
actions, given as a command in code. A statement ends with a semi-colon (;). An example structure of Java is
shown below.

public static void main(String[] args)

// statements

Page 11 Quarter 2 | Week 3

You might also like