Disusun Oleh :
NIM : 203210022
JENJANG : D3
STIMIK AKAOM
YOGYAKARTA
2021
PERTEMUAN KE-02
PERULANGAN BERTINGKAT DUA DAN TIGA
A. TUJUAN
Mahasiwa dapat membuat program untuk menyelesaikan kasus
menggunakan perulangan bertingkat 1 maupun 3.
B. PRAKTIK DAN PEMBAHASAN
1. Buatlah program di bawah ini dan amati hasil outputnya, gantilah
I<=3 dan j<=2 bagaimana hasil outputnya?
Program
1. public class Looping{
2.
3. public static void main(String[] args){
4. for (int i=1; i<=2;i++ ) {
5.
6. for (int j=1;j<=3 ;j++ ) {
7. System.out.format("Perulangan [i=%d, j=%d] %n", i,j);
8. }
9. }
10. }
11. }
Praktik
Output
Gambar 1
Gambar 2
2. Salinlah program dibawah ini dan amati hasil outputnya
Program
1. public class Bentuk1
2. {
3. public static void main(String[] args)
4. {
5. int a = 5;
6. for (int b = 1; b <= a; b++)
7. {
8. System.out.print('*');
9. System.out.println();
10. }}}
Praktik
Output
3. Modifikasi program nomor 2, untuk menghasilkan output sebagai
berikut :
*
**
***
****
*****
Press any key to continue. . .
Program
1. public class Bentuk2
2. {
3. public static void main(String[] args) {
4. int a = 5;
5. for (int b = 1; b <= a; b++){
6. for (int c = 1; c <= b; c++) {
7. System.out.print('*');
8. }
9. System.out.println();
10. }
11. }}
Praktik
Output
***
*****
*******
*********
Program
1. public class Bentuk3
2.
3. {
4. public static void main(String[] args)
5. {
6. int x = 5;
7. for (int i = 1; i <= x; i++)
8. {
9. for (int j = 4; j >= i; j--)
10. {
11. System.out.print(' ');
12. }
13. for (int k = 1; k <= i; k++)
14. {
15. System.out.print('*');
16. }
17. for (int l = 1; l <= i - 1; l++)
18. {
19. System.out.print('*');
20. }
21. System.out.println();
22. }}}
Praktik
Output
6. Modifikasi praktik program 2 dan 5 dengan menggunakan perulangan
while dan modifikasi praktik program no 3 dan 4 dengan
menggunakan perulangan do while.
a. Perulangan while
1. Praktik 2
program
1. public class Bentuk2_modif
2. {
3. public static void main(String[] args) {
4. int a = 5;
5. int b = 1;
6. do {
7. int c = 1;
8. do {
9. System.out.print('*');
10. c++;
11. }while( c <= b );
12. System.out.println();
13. b++;
14.
15. }while( b <= a );
16. }}
Praktik
Output
2. Praktik 5
Program
1. public class Bentuk3a_modif
2. {
3. public static void main(String[] args)
4. {
5. int x=5,i,j,k,l;
6. i=1;
7. while ( i <= x )
8. {
9. j = 4;
10. k = 1;
11. l = 1;
12. while( j >= i)
13. {
14. System.out.print(' ');
15. j--;
16. }
17. while ( k <= i )
18. {
19. System.out.print(k);
20. k++;
21. }
22. while ( l <= i - 1 )
23. {
24. System.out.print(i);
25. l++;
26. }
27. System.out.println();
28. i++;
29. }}}
Program
Output
b. Perulangan do while
1. Praktik 3
Program
1. public class Bentuk3a_modif
2. {
3. public static void main(String[] args)
4. {
5. int x=5,i,j,k,l;
6. i=1;
7. while ( i <= x )
8. {
9. j = 4;
10. k = 1;
11. l = 1;
12. while( j >= i)
13. {
14. System.out.print(' ');
15. j--;
16. }
17. while ( k <= i )
18. {
19. System.out.print(k);
20. k++;
21. }
22. while ( l <= i - 1 )
23. {
24. System.out.print(i);
25. l++;
26. }
27. System.out.println();
28. i++;
29. }}}
Program
Output
Output
Praktik
Output
3. Berdasarkan latihan program 2 buatlah sebuah program untuk
menghasilkan output sebagai berikut :
Perulangan bersarang membentuk pola
*****akakom
****akakom
***akakom
**akakom
*akakom
Press any key continue. . .
Program
1. public class Pola3 {
2. public static void main(String [] args){
3. int i,j,x,k=5;
4. for(i=0;i<k;i++){
5. for(j=0;j<i;j++){
6. System.out.print(" ");
7. }
8. int s=k-i;
9. for(x=0;x<s;x++){
10. System.out.print("*");
11. }
12. System.out.println("akakom");
13. }
14. }
15. }
16.
Praktik
Output
E. KESIMPULAN
F. LAMPIRAN LISTING
a. Praktik
1.
2.
3.
4.
5.
6.
7.
8.
b. Latihan
1.
2.
3.
c. Tugas
1.