You are on page 1of 4

CAP

Information Technology E
Syllabus Focus: Unit 1 Module 3 Content 10
Specific Objective 10: develop algorithms to represent problem solution;

Content: Simple input, output, processing; control structures: sequence, selection, looping and
iteration.

The building blocks of algorithms


An algorithm is a step by step process that describes how to solve a problem
in a way that always gives a correct answer. When there are multiple
algorithms for a particular problem (and there often are!), the best algorithm
is typically the one that solves it the fastest.

As computer programmers, we are constantly using algorithms, whether it's


an existing algorithm for a common problem, like sorting an array, or if it's a
completely new algorithm unique to our program. By understanding
algorithms, we can make better decisions about which existing algorithms to
use and learn how to make new algorithms that are correct and efficient.

An algorithm is made up of three basic building blocks: sequencing,


selection, and iteration.

Sequencing: An algorithm is a step-by-step process, and the order of those


steps are crucial to ensuring the correctness of an algorithm.

Here's an algorithm for translating a word into Pig Latin, like from "pig" to
"ig-pay":

1. Append "-".

2. Append first letter

3. Append "ay"

4. Remove first letter


🔍 Try following those steps in different orders and see what comes out. Not
the same, is it?

Selection: Algorithms can use selection to determine a different set of steps


to execute based on a Boolean expression.

Here's an improved algorithm for Pig Latin that handles words that starts with
vowels, so that "eggs" becomes "eggs-yay" instead of the unpronounceable
"ggs-eay":

1. Append "-"

2. Store first letter

3. If first letter is vowel:

a. Append "yay"

4. Otherwise:

a. Append first letter

b. Append "ay"

c. Remove first letter

Iteration: Algorithms often use repetition to execute steps a certain number


of times or until a certain condition is met.

We can add iteration to the previous algorithm to translate a complete phrase,


so that "peanut butter and jelly" becomes "eanut-pay utter-bay and-yay elly-
jay":

1. Store list of words

2. For each word in words:

a. Append hyphen

b. If first letter is vowel:

i. Append "yay"

c. Otherwise:
i. Append first letter

ii. Append "ay"

iii. Remove first letter


taken from : https://www.khanacademy.org/computing/ap-computer-science-principles/
algorithms-101/building-algorithms/a/the-building-blocks-of-algorithms , on September 12, 2020

Iteration Control Structures


Kenneth Leroy Busbee and Dave Braunschweig

Overview
In iteration control structures, a statement or block is executed until the program
reaches a certain state, or operations have been applied to every element of a
collection. This is usually expressed with keywords such as while, repeat, for,
or do..until.[1]

Discussion

The basic attribute of an iteration control structure is to be able to repeat some lines of
code. The visual display of iteration creates a circular loop pattern when flowcharted,
thus the word “loop” is associated with iteration control structures. Iteration can be
accomplished with test before loops, test after loops, and counting loops. A question
using Boolean concepts usually controls how often the loop will execute.
Iteration (Repetition) Control Structures

pseudocode: While
count assigned zero

While count < 5

Display "I love computers!"

Increment count

End

pseudocode: Do While
count assigned five

Do

Display "Blast off is soon!"

Decrement count

While count > zero

pseudocode: Repeat Until


count assigned five

Repeat

Display "Blast off is soon!"

Decrement count

Until count < one

pseudocode: For
For x starts at 0, x < 5, increment x

Display "Are we having fun?"

End

Taken from: https://press.rebus.community/programmingfundamentals/chapter/iteration-control-


structures/, on September 12, 2020

Additional Sources:
 https://webcache.googleusercontent.com/search?q=cache:bcUdjSPgtQsJ:https://
raptor.martincarlisle.com/F-Programming%2520Control
%2520Structures.doc+&cd=1&hl=en&ct=clnk&gl=jm
 https://www.unf.edu/~broggio/cop2221/2221pseu.htm

You might also like