Documente Academic
Documente Profesional
Documente Cultură
Terminal Decision
Input/Output
Operation Connector
Process Module
Flowcharts
•So we start the program as:
START
Flowcharts
•Our program will finish with the following:
END
SEQUENCE
Flowcharts
•When we write programs, we assume that the
computer executes the program starting at the
beginning and working its way to the end.
•This is a basic assumption of all algorithm
design.
Flowcharts
•When we write programs, we assume that the
computer executes the program starting at the
beginning and working its way to the end.
•This is a basic assumption of all algorithm
design.
•We call this SEQUENCE.
START
Statement1
Statement2
END
SELECTION
Flowcharts
•What if we want to make a choice, for example,
do we want to add sugar or not to the tea?
Flowcharts
•What if we want to make a choice, for example,
do we want to add sugar or not to the tea?
•We call this SELECTION.
START
Yes Is Sugar No
Add Sugar Don’t Add Sugar
required?
END
ITERATION
Flowcharts
•What if we need to tell the computer to keep
doing something until some condition occurs?
Flowcharts
•What if we need to tell the computer to keep
doing something until some condition occurs?
•Let’s say we wish to indicate that the you need
to keep filling the kettle with water until it is full.
Flowcharts
•What if we need to tell the computer to keep
doing something until some condition occurs?
•Let’s say we wish to indicate that the you need
to keep filling the kettle with water until it is full.
•We need a loop, or ITERATION.
START
Kettle is No
not full?
Yes
END
EXAMPLES
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and print it out.
START
START
Read in A
START
Read in A
Print A
START
Read in A
Print A
END
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and print it out double the number.
START
START
Read in A
START
Read in A
Print A*2
START
Read in A
Print A*2
END
Or alternatively...
START
START
Read in A
START
Read in A
B = A*2
START
Read in A
B=A*2
can be
B = A*2 read as
“B gets the
value of A
multiplied by 2”
START
Read in A
B = A*2
Print B
START
Read in A
B = A*2
Print B
END
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number, check if it is odd or even.
START
START
Read in A
START
Read in A
Does A/2
give a
remainder?
START
Read in A
Does A/2
Print “It’s Odd” Yes
give a
remainder?
START
Read in A
Does A/2 No
Print “It’s Odd” Yes Print “It’s Even”
give a
remainder?
START
Read in A
Does A/2 No
Print “It’s Odd” Yes Print “It’s Even”
give a
remainder?
END
Flowcharts
•So let’s say we want to express the
following algorithm to print out the bigger of
two numbers:
– Read in two numbers, call them A and B. Is A is bigger
than B, print out A, otherwise print out B.
START
START
Read in A and B
START
Read in A and B
A>B?
START
Read in A and B
Print A Yes
A>B?
START
Read in A and B
Yes No
Print A A>B? Print B
START
Read in A and B
Yes No
Print A A>B? Print B
END
Flowcharts
•So let’s say we want to express the
following algorithm to print out the bigger of
three numbers:
– Read in three numbers, call them A, B and C.
• If A is bigger than B, then if A is bigger than C, print out A,
otherwise print out C.
• If B is bigger than A, then if B is bigger than C, print out B,
otherwise print out C.
START
START
Read in A, B and C
START
Read in A, B and C
A>B?
START
Read in A, B and C
Yes
A>C? A>B?
START
Read in A, B and C
Yes No
A>C? A>B? B>C?
START
Read in A, B and C
Yes No
A>C? A>B? B>C?
No No
Print C
START
Read in A, B and C
Yes Yes No
A>C? A>B? B>C?
No No
Print A Print C
START
Read in A, B and C
No No
Read in A, B and C
No No
END
Flowcharts
•So let’s say we want to express the
following algorithm:
– Print out the numbers from 1 to 5
START
START
Print 1
START
Print 1
Print 2
START
Print 1
Print 2
Print 3
START
Print 1
Print 2
Print 3
Print 4
START
Print 1
Print 2
Print 3
Print 4
Print 5
START
Print 1
Print 2
Print 3
Print 4
Print 5
END
Or alternatively...
Flowcharts A=A+1
15
A (new)
START
START
A=1
START
A=1
Is A==6?
START
A=1
No
Is A==6? Print A
START
A=1
A=A+1
No
Is A==6? Print A
START
A=1
A=A+1
No
Is A==6? Print A
START
A=1
A=A+1
No
Is A==6? Print A
Yes
END
Flowcharts
•So let’s say we want to express the
following algorithm:
– Add up the numbers 1 to 5
Flowcharts T=5
T
Flowcharts T=5
T
Flowcharts T=X
14
X
14
T
Flowcharts T=X+1
+1 14
X
15
T
Flowcharts T=T+X
Total = 0
START
Total = 0
A=1
START
Total = 0
A=1
Is A==6?
START
Total = 0
A=1
No
Is A==6? Total = Total + A;
START
Total = 0
A=1
A=A+1
No
Is A==6? Total = Total + A;
START
Total = 0
A=1
A=A+1
No
Is A==6? Total = Total + A;
Yes
END
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and check if it’s a prime number.
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and check if it’s a prime number.
– What’s a prime number?
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and check if it’s a prime number.
– What’s a prime number?
– A number that’s only divisible by itself and 1, e.g. 7.
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and check if it’s a prime number.
– What’s a prime number?
– A number that’s only divisible by itself and 1, e.g. 7.
– Or to put it another way, every number other than itself and 1
gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder
then 7 is prime.
Flowcharts
•So let’s say we want to express the
following algorithm:
– Read in a number and check if it’s a prime number.
– What’s a prime number?
– A number that’s only divisible by itself and 1, e.g. 7.
– Or to put it another way, every number other than itself and 1
gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder
then 7 is prime.
– So all we need to do is divide 7 by all numbers less than it but
greater than one, and if any of them have no remainder, we know
it’s not prime.
Flowcharts
•So,
•If the number is 7, as long as 6, 5, 4, 3, and
2 give a remainder, 7 is prime.
•If the number is 9, we know that 8, 7, 6, 5,
and 4, all give remainders, but 3 does not
give a remainder, it goes evenly into 9 so we
can say 9 is not prime
Flowcharts
•So remember,
– if the number is 7, as long as 6, 5, 4, 3, and 2
give a remainder, 7 is prime.
•So, in general,
– if the number is A, as long as A-1, A-2, A-3, A-
4, ... 2 give a remainder, A is prime.
START
START
Read in A
START
Read in A
B = A -1
START
Read in A
B = A -1
Is B = = 1?
START
Read in A
B = A -1
No
Is B = = 1?
START
Read in A
B = A -1
No
Is B = = 1?
Does
A/B give a
remainder?
START
Read in A
B = A -1
No
Is B = = 1?
Yes
Does
A/B give a
remainder?
START
Read in A
B = A -1
B=B-1 No
Is B = = 1?
Yes
Does
A/B give a
remainder?
START
Read in A
B = A -1
B=B-1 No
Is B = = 1?
Yes
Does
A/B give a
remainder?
START
Read in A
B = A -1
B=B-1 No
Is B = = 1?
Yes
Does
A/B give a
remainder?
No
Read in A
B = A -1
B=B-1 No Yes
Is B = = 1? Print “Prime”
Yes
Does
A/B give a
remainder?
No
Read in A
B = A -1
B=B-1 No Yes
Is B = = 1? Print “Prime”
Yes
Does
A/B give a
remainder?
No
END
Tugas: Buat Flowchart FPB
dan KPK