Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

ALGORITHM AND

PROGRAMMING
Session 2

Anita Safitri, M.Kom.


• Descriptive

Algorithm Form • Flowchart


• Psuedocode

Anita Safitri, M.Kom.


Writing Algorithm
Algorithm can be express in 3 different way

• Descriptive
• Flowchart
• Psuedocode

Anita Safitri, M.Kom.


Descriptive
• Writing algorithm in the form of descriptions or narratives.
• E.g. algorithm to display number 1-9

Output : 1,2,3….,9
Algorithm …
…. Create variable “bilangan”
…. Set the value 1 to the bilangan variable
…. Repeat as long as bilangan is less than 10
…………… show bilangan
…………… adjust value bilangan by 1

Anita Safitri, M.Kom


Descriptive Algorithm (1)
• An example of an algorithm for calculating the area of a circle by entering the
radius of the circle. In general, it can be solved by the following steps.

1. input value of radius


2. count area by using formula : 3.14*radius*radius
3. display area

Anita Safitri, M.Kom.


Flowchart
Writing algorithm in flow diagram that describes a process.

Flowchart can be grouped into two categories:


• System Flowchart. Usually used by systems analysts to describe the data
flow or file structure within a system
• Program Flowchart. commonly used by programmers to explain the steps in
a program

Anita Safitri, M.Kom.


Flowchart (1)

Anita Safitri, M.Kom.


Flowchart (2)

Anita Safitri, M.Kom.


Pseudocode
• Description of a computer programming algorithm that uses a simple structure so
that it can easily be read by humans.
• The algorithm consists of three parts, namely:
Title (Header): defines the name by determining whether the text is a program,
procedure, function.
Declaration: defines the name of the variable, the name of the constant, the name of the
procedure, the name of the function that will be used in the algorithm.
Description: defines the steps for solving the problem starting from input, process and
output.
In each of these sections, if you want to write comments about each section, it is written
between curly brackets Example: {Comments}

Anita Safitri, M.Kom.


Basic Logic • Sequential

Structure • Selection
• Loop

Anita Safitri, M.Kom.


Sequential
• Instructions are executed Algorithm Area_of_Circle
sequentially. done one by one
according to the order of the
command Declaration
radius, area : integer
Instruction 1
Begin
1. Input value of radius
Instruction 2
2. Count area ← 3.14 * radius *radius
3. Write (area)
Instruction 3
end

Anita Safitri, M.Kom.


Sequential (1)
• Exercise! Create an algorithm to
switch number of two variables !

A B

Anita Safitri, M.Kom.


Selection
• Selection In a selection structure, the command is executed when the conditions are
met
• E.g. Algorithm determines the larger number of two varables.
A, B A, B

False True
A>B True A>B

False Output A Output B Output A

Anita Safitri, M.Kom.


Selection (1)
If-then selection If-then-else selection
Algorithm Positive_Number Algorithm PositiveNegative_Number
Declaration Declaration
x : integer x : integer
ket : string ket : string
begin
Input x Begin
If x > 0, then Input x
ket ← ‘positive number’
write (ket) If x > 0, then
ket ← (‘positive number’)
end
else
ket ← (‘negative number’)
write (ket)
end
Anita Safitri, M.Kom.
Selection (3)
• Exercise! Write an algorithm that
determines whether a number is odd
or even.

Odd?
Number Even?

Anita Safitri, M.Kom.


Loop
• A loop is a sequence that gets executed Algorithm Display_threetimes.
several times. A complete execution of a
sequence is called an iteration of the loop
• For example, if you want to display the Begin
words “Algoritma” three times, then the Write (“Algoritma”)
algorithm can be written:
Write (“Algoritma”)
Write (“Algoritma”)
End

How If we want to display the words in


hundred or thousand times?

Anita Safitri, M.Kom.


Loop (1)
For – Do Loop
Algorithm Display_threetimes Algorithm Display_threetimes
Declaration Declaration
i = integer i = integer
Begin Begin
For i ← 1 to 3 do For i ← 3 downto 1 do
write (‘Halo’) write (‘Halo’)
End End

Anita Safitri, M.Kom.


Loop (2)

General Form
For variable ← starting_value to ending_value do
action

OR

For variable ← starting_value downto ending_value do


action
Anita Safitri, M.Kom.
Loop (3)
While – Do Loop
Algorithm Series

Declaration General Form


n,x = integer while <condition> do
Begin
read (n) action
x←2
while x <= n do
write (x)
x←x+2
End

Anita Safitri, M.Kom.


Loop (4)
Repeat – Until Loop
Algorithm Series
General Form
Declaration
x, n = integer repeat
Begin
read (n) action
x←2 until <condition>
repeat
write (x)
x←x+2
until x > n
End Anita Safitri, M.Kom.
Loop (5)
• Exercise! Write an algorithm that
shows the sum of the previous series.

Anita Safitri, M.Kom.


Exercise!
• Make an algorithm determine the value that must be paid by the consumer. on the
condition that if the purchase is greater than 100 thousand, you will get a 10%
discount and if it is greater than 500 thousand you will get a 30% discount.

Anita Safitri, M.Kom.


THANKYOU

Anita Safitri, M.Kom.

You might also like