![Page 1: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/1.jpg)
R E P E T I T I O N
Pemrograman Dasar
![Page 2: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/2.jpg)
Pengulangan 2
Struktur pengulangan dimaksudkan untuk
melakukan proses pengulangan dari
beberapa instruksi dalam sejumlah
pengulangan tertentu
Tiga macam struktur pengulangan dalam
pemrograman terstruktur, yaitu: Struktur for
Struktur while
Struktur do-while
![Page 3: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/3.jpg)
Pengulangan FOR
3
Konstruksi for
for (exp1; exp2; exp3)
statement;
atau
for (exp1; exp2; exp3) {
statement1;
statement2;
…
}
exp1 : inisialisasi untuk variabel yang mengontrol
pengulangan exp2 : kondisi (true:diulang, false:berhenti) exp3 : increment or decrement of control variable(s) keberadaan exp1, exp2, dan exp3 bersifat optional
![Page 4: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/4.jpg)
Pengulangan FOR
4
Struktur logika pengulangan for
exp1
exp3
statements
exp2 true
false
exp1
exp3
statements
exp2
false
![Page 5: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/5.jpg)
Pengulangan FOR 5
Contoh :
byte x;
for (x=1; x <= 10; x++)
System.out.printf("%d\n",x);
x = 1
x++
printf(“%d\n”,x)
x<=10 true
false
![Page 6: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/6.jpg)
Pengulangan FOR
6
Contoh :
Program mencetak angka dari 1 sampai 10
Program mencetak angka dari 10 sampai 1
byte x;
for (x=1; x <= 10; x++)
System.out.printf("%d\n",x);
byte x;
for(x=10; x>=1; x--)
System.out.printf("%d\n",x);
![Page 7: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/7.jpg)
Pengulangan FOR
7
exp1 dan exp3 boleh terdiri dari beberapa ekspresi
yang dipisahkan dengan koma.
Contoh
byte i,j; for(i=1, j=30; i<j; i++, j--){
System.out.printf("%04d -- %04d\n", i,j);
}
System.out.printf("\n\n");
0001 -- 0030
0002 -- 0029
0003 -- 0028
0004 -- 0027
0005 -- 0026
0006 -- 0025
0007 -- 0024
0008 -- 0023
0009 -- 0022
0010 -- 0021
0011 -- 0020
0012 -- 0019
0013 -- 0018
0014 -- 0017
0015 -- 0016
![Page 8: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/8.jpg)
Pengulangan FOR 8
Infinite Loop Infinite loop dapat diimplentasikan dengan menghilangkan ketiga parameter (exp1, exp2, exp3) pada for statement. break statement membuat keluar dari loop.
Contoh: byte i = 0;
for(;;){
i++;
System.out.printf("%d ", i);
if (i>=10) break;
}
System.out.printf("\n\n");
1 2 3 4 5 6 7 8 9 10
![Page 9: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/9.jpg)
Pengulangan FOR 9
Nested Loop
Nested loop disebut juga dengan pengulangan
bersarang. Pengulangan dalam pernyataan
pengulangan.
Contoh:
Output :
1 5 1 4 1 3 .. 2 5 2 4 .. 5 1
byte x, y;
for (x=1;x<=5;x++)
for (y=5; y>=1; y--)
System.out.printf("%d %d ",x,y);
![Page 10: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/10.jpg)
Pengulangan WHILE 10
Sintaks :
while (boolean exp) statement;
atau
while(boolean exp){
statement1;
statement2;
..
}
Boolean exp
statements
true
false
![Page 11: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/11.jpg)
Pengulangan WHILE 11
Contoh :
while(product <= 1000)
product = 2*product;
product <= 1000
product=2*product;
false
true
![Page 12: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/12.jpg)
Pengulangan WHILE
12
Contoh Program:
int n,i,f;
i = n; f = n;
while (i>1){
i--;
f=f*i;
}
printf("%d! = %d\n\n",n,f);
![Page 13: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/13.jpg)
Pengulangan WHILE
13
Kesetaraan for dengan while sbb:
int x;
for(x=1;x<=10;x++)
printf("%d\n", x);
exp1;
while(exp2){
statement1;
statement2;
….
exp3
}
for(exp1; exp2; exp3){
statement1;
statement2;
….
}
setara
serupa
int x=1;
while(x<=10){
printf("%d\n", x);
x++;
}
![Page 14: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/14.jpg)
Pengulangan DO-WHILE 14
Sintaks :
do statement while (boolean exp);
atau
do{
statement1;
statement2;
..
} while(boolean exp)
eks. boolean
true
false
statements
![Page 15: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/15.jpg)
Pengulangan DO-WHILE 15
Contoh :
int i = 0;
do{
printf(“%d\n”, i);
} while(++i <=10);
++i <= 10 true
false
i = 0
Cetak i
![Page 16: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/16.jpg)
Catatan 16
Pada pengulangan while, statement atau
block statement mungkin tidak akan pernah
dilaksanakan bila nilai ekspresi boolen false,
karena operasi pengulangan diawali dengan
mengeksekusi ekspresi boolean.
Pada pengulangan do-while statement atau
block statement pasti dikerjakan minimal satu
kali, karena ekspresi boolean baru diuji pada
akhir blok pengulangan.
![Page 17: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/17.jpg)
Break dan Continue 17
break
keluar dari pengulangan (for, while dan do-while) atau seleksi (switch)
continue
tinggalkan sisa instruksi dalam sebuah pengulangan dan masuki tahap pengulangan selanjutnya
![Page 18: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/18.jpg)
Break 18
Contoh penggunaan break pada pengulangan:
int x = 1;
while(x<=10){
printf("%d\n", x);
x++;
if (x>5)
break;
} keluar dari loop
![Page 19: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/19.jpg)
Continue 19
Contoh penggunaan continue pada pengulangan:
int x;
for(x=1; x<=10; x++) {
if (x == 5)
continue;
printf("%d ", x);
}
![Page 20: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/20.jpg)
Break vs Continue 20
do {
x = input.nextFloat();
if(x<0) {
printf(”\nError. Negative”);
break;
}
/*Proses nonnegatif */
…
} while(exp);
do {
x = input.nextFloat();
if(x<0) {
printf”\nError. Negative”);
continue;
}
/*Proses nonnegatif*/
…
} while(exp);
![Page 21: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/21.jpg)
Tugas 21
1. Buat algoritma untuk menampilkan bilangan ganjil dari 11 sampai 188, dengan menggunakan :
for, while, dan do - while
2. Diasumsikan hari 1 menyatakan senin, 2 - selasa, 3 - rabu,…, 7 – minggu. Buatlah algoritma untuk menampilkan angka hari sebanyak n yang diinput dari keyboard. Perhatikan pola berikut :
n = 3 1 2 3
n = 7 1 2 3 4 5 6 7
n = 10 1 2 3 4 5 6 7 1 2 3
![Page 22: A Glimpse of Software Development Lifecycleblog.ub.ac.id/insanilah/files/2013/12/ProgDas07-Pengulangan.pdf · Pengulangan 2 Struktur pengulangan dimaksudkan untuk melakukan proses](https://reader036.vdocuments.pub/reader036/viewer/2022071102/5fdb048e77ef9e2e4163c4b4/html5/thumbnails/22.jpg)
Tugas 22
3. Buatlah algoritma untuk menampilkan gambar
seperti di bawah ini, dengan n variabel integer yang
nilainya didapat dari keyboard input. Gunakan for,
while atau do-while.
*
* *
* * *
* * * *
* * * * *
n
n