13/1/2020
MTES3113
Struktur Kawalan/
Control Structure
@DL IPGKPP 2018
Basically
commands in
code form
… Java, COBOL,
Arahan/Perintah FORTRAN,
pada asasnya PASCAL, BASIC,
dalam bentuk Visual Basic,
PYTHON
kod
@DL IPGKPP 2020 @DL IPGKPP 2020
Kawalan adalah penentu aliran There are 3 types of control
perlaksanaan arahan dalam structure
aturcara. sequence control
• Bermula dengan arahan pertama, kawalan akan decision control
menentukan arahan manakah yang akan
loop control
dilaksanakan seterusnya sehingga tamat
Control determines the command Terdapat 3 jenis struktur kawalan
implementation flow in programming Kawalan jujukan
Starts with the first instruction, the structure will Kawalan pilihan
determines which instruction will be implemented Kawalan ulangan
next until the end
@DL IPGKPP 2020 @DL IPGKPP 2020
1
13/1/2020
Kenyataan dilakukan baris demi Mula Start
baris mengikut urutan /
Statements are carried out row by Minta pengguna input Ask user to input 3
3 nombor numbers
row in a sequence
Cari hasil tambah 3 Find the sum of 3
Digunakan apabila nombor numbers
arahan berdasarkan Bahagikan hasil
Divide the sum with 3
urutan demi urutan/ tambah dengan 3
Is used when
Cetak Print
instruction is based
on rows
Tamat End
@DL IPGKPP 2020 @DL IPGKPP 2020
Start
Start
Output “ Enter the
Output “ Enter your radius value:”
name:”
Input radius
Input name
Compute
Output “ Hello” + Circle Area =
name PI*radius*radius
End Output “Circle Area =
“ + answer
End
@DL IPGKPP 2020 @DL IPGKPP 2020
Kenyataan dilakukan
mengikut keadaan/syarat
(syarat ialah benar atau
Start palsu)
Output “ Enter the Statement is executed
radius value:”
based on the condition if it
Input radius is true or false
Compute
Circle Area = Digunakan apabila mahu
PI*radius*radius
membuat keputusan dalam
Output “Circle Area = perjalanan sesuatu
“ + answer
program (mungkin
End mengakibatkan lompatan)
Is used when needed to
make a decision (maybe it
may cause a jump) @DL IPGKPP 2020
@DL IPGKPP 2020
2
13/1/2020
Menggunakan simbol
pilihan
Uses a decision symbol Adakah
benar Papar Is sum
hasil
“Gagal” <10?
<10?
KP tunggal, KP dwipilihan true
dan KP pelbagai pilihan palsu Display false
“Fail”
If, if----else, switch
Papar Display
Single DC, Dual DC and “Lulus” “Pass”
multi ways DC
If, if----else, switch
@DL IPGKPP 2020 @DL IPGKPP 2020
Kenyataan “if……”
Jika keadaan benar tindakan dilakukan
Jika keadaan palsu, tindakan dilangkau
“if……” statement
If condition true, action will be taken
If condition false, action will skip
Kenyataan “if/else”
Tindakan berbeza jika keadaan benar atau palsu
“if/else” statement
Different action if condition is true or false
@DL IPGKPP 2020 @DL IPGKPP 2020
Kenyataan “switch”
true
Membolehkan satu pemboleh ubah untuk diuji case a case a
action(s)
break
berbanding dengan satu senarai nilai-nilai. Setiap false
nilai dipanggil kes dan pemboleh ubah di ‘switched
on’ untuk disemak untuk setiap kes.
true
case b case b break
action(s)
“switch” statement false
Enable one variable to be compared and tested with .
.
a list of values. Each value is called case and the .
variable is ‘switched on’ to check for each case.
true
case z case z break
action(s)
false
default
action(s)
@DL IPGKPP 2020 @DL IPGKPP 2020 18
3
13/1/2020
Atarcara yang dibuat perlu KU “SELAGI”
mengulang pemprosesan
yang sama beberapa kali
IC “WHILE”
sehingga satu Tamat
?
keadaan/syarat dipenuhi /End
KU ULANG-
Programs that to be SEHINGGA
iterated its same processes
several times until one IC repeat/until;
condition is satisfied. do/while
KU “UNTUK”
IC “FOR”
@DL IPGKPP 2020 @DL IPGKPP 2020
Adalah lebih baik untuk melaksanakan satu Ini dapat dilaksanakan dengan satu struktur
operasi berulang dengan menulis kod untuk ulangan, yang lebih dikenali sebagai satu
operasi tersebut sekali dan kemudian loop
letakkannya dalam struktur supaya memaksa This can be completed withone iteration
komputer mengulanginya sebanyak kali yang structure, which is better known as a loop
perlu daripada menulis kod ikut kawalan
jujukan.
Program Payroll untuk pekerja.
• perlu lakukan langkah yang sama untuk setiap orang pekerja
It is better to implement one iteration • Menyelesaikan masalah begini memerlukan satu struktur yang
operation by writing a code for the operation diulangi satu set arahan/kenyataan sebanyak kali yang perlu
once and then put it inside the structure so Workers Payroll Program.
that it forces the computer to repeat it as • Needs to repeat the same steps for each worker
many times needed then to write a code • Solving problem like this required one structure where a set of
following a sequence control command/statements is repeated as many times needed.
@DL IPGKPP 2020 @DL IPGKPP 2020
Masukkan satu Enter one positive
nombor positif number
Adakah Is the
nombor benar number True
positif? positive?
Tamat End
palsu False
Input semula Reinput
@DL IPGKPP 2020 @DL IPGKPP 2020
4
13/1/2020
1. Set nilai permulaan untuk pembilang sebelum ulangan bermula/ Bentuk/Form:
A set of starting values for the counter before looping starts
2. Biasanya ada satu pembilang tokokan selepas ulangan bermula/ while (syarat/condition)
Normally there is one incremental counter after the loop started kenyataan/statement
3. Contoh: Cetak “Hello” 5 kali
Example: Print “Hello” 5 times
Jika keadaan benar, maka kenyataan
dilakukan, kemudian semua
dilaksanakan semula sehingga
Control-variable
expression
4. Carta alir/ Flow Chart: false
keadaan menjadi palsu
If condition is true, then statement
Adjustment Continue
expression condition?
true
is executed, then all executed again
until condition becomes false
Statement(s)
(loop-body)
Next
Statement @DL IPGKPP 2020 @DL IPGKPP 2020
Kod Pseudo: 1. Carta Alir/Flow Chart
While (selagi) terdapat lagi barangan pada senarai shopping
Beli barang yang berikutnya dan pangkahkan daripada
senarai saya condition? true statement
Pseudo Code:
false
While there are still things in the shopping list
Buy the following things and cross out from my list 2. Contoh/ Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
true
product <= 1000 product = 2 * product
false
@DL IPGKPP 2020 @DL IPGKPP 2020
seconds = 5; Serupa dengan struktur while
while (seconds > 0) { Buat pengujian penyambungan ulangan pada
System.out.print(seconds + "..."); penghujung dan bukan pada awalan
seconds = seconds - 1; Badan loop dilakukan sekurang-kurangnya sekali
} “Bottom tested loop” Statement(s)
System.out.println("Blast off!"); Similar to while structure
Does connecting iteration testing
OUTPUT:
true
at the end and not at the beginning Continue
condition?
5...4...3...2...1...Blast off! The loop body is executed at least once
“Bottom tested loop”
false
Next
Statement
@DL IPGKPP 2020 @DL IPGKPP 2020
5
13/1/2020
Statement(s)
Format
do { true
Continue
statement condition?
} while ( continue condition );
false
Next
Statement
@DL IPGKPP 2020 @DL IPGKPP 2020