Anda di halaman 1dari 26

Repetition Statements (loops)

“while and do-while”


Aji Seto Arifianto, S.ST, M.T

Pemrograman Dasar

1
Perulangan (loops)
Perulangan dalam pemrograman dibagi manjadi dua jenis:
• Counted loop : Perulangan yang jumlah pengulangannya terhitung
atau tentu.
• for (dibahas di minggu 10) dan for each (dibahas jika sudah mengenal
Array)
• Uncounted loop : Perulangan yang jumlah pengulangannya tidak
terhitung atau tidak tentu.
• while dan do-while

Pemrograman Dasar 2
for vs while vs do-while
• For  Jika kita tahu berapa kali akan melakukan perulangan (loop)
• Misal : 10 kali / 100 kali / dari 1-30
• While  Jika kita tidak tahu berapa kali akan melakukan
perulangan, tetapi tahu kapan harus berhenti
• Misal : diulang terus sampai nama/string yang dimasukkan benar

Pemrograman Dasar 3
While vs Do/While
• Perulangan while akan melakukan perulangan kalau kondisi
(syarat) terpenuhi/true.

• Sedangkan do-while melakukan perulangan dulu, kemudian


memeriksa kondisinya atau sayaratnya.
• Kalau kondisi terpenuhi, maka do-while akan melanjutkan
perulangan. Sebaliknya, dia akan berhenti (break).

Pemrograman Dasar 4
The while Statement
• A while statement has the following syntax:

while ( condition )
statement;
• If the condition is true, the statement is executed
• Then the condition is evaluated again, and if it is still true, the
statement is executed again
• The statement is executed repeatedly until the condition becomes
false

Pemrograman Dasar 5
Logic of a while Loop
• while akan
melakukan
condition
perulangan kalau evaluated
kondisi (syarat)
terpenuhi/true
false
• Dan akan true

berhenti, jika
kondisinya tidak statement

terpenuhi/false

Pemrograman Dasar 5
The while Statement
• An example of a while statement:
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
• If the condition of a while loop is false initially, the statement is
never executed
• Therefore, the body of a while loop will execute zero or more times

Pemrograman Dasar 7
Trace while Loop

Initialize count
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 7
Trace while Loop, cont.

(count < 2) is true


int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 8
Trace while Loop, cont.

Print Welcome to Java


int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 9
Trace while Loop, cont.

Increase count by 1
int count = 0; count is 1 now
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 10
Trace while Loop, cont.

(count < 2) is still true since count


int count = 0; is 1
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 11
Trace while Loop, cont.

Print Welcome to Java


int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 12
Trace while Loop, cont.

Increase count by 1
int count = 0; count is 2 now
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 13
Trace while Loop, cont.

(count < 2) is false since count is 2


int count = 0; now
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 14
Trace while Loop
The loop exits. Execute the next
int count = 0; statement after the loop.
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

Pemrograman Dasar 15
Example (Average.java)

Pemrograman Dasar 17
Infinite Loops (Perulangan tidak terbatas)
• Executing the statements in the body of a while loop must eventually
make the condition false
• If not, it is called an infinite loop, which will execute until the user
interrupts the program
• This is a common logical error
• You should always double check the logic of a program to ensure
that your loops will terminate

Pemrograman Dasar 18
Infinite Loops (Perulangan tidak terbatas)
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
System.out.println(count);
count = count - 1;
}
• This loop will continue executing until the user externally
interrupts the program.

Pemrograman Dasar 19
Nested Loops
• Similar to nested if statements, loops can be nested as well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates
completely

Pemrograman Dasar 20
Nested Loops
• How many times will the string "Here" be printed?

count1 = 1;
while (count1 <= 10)
{
count2 = 1; Answer :
while (count2 <= 20) 10 * 20 = 200
{
System.out.println ("Here");
count2++;
}
count1++;
}

Pemrograman Dasar 21
The do-while Statement
• A do-while statement has the following syntax:
do{
statement; Untuk do-while
menggunakan simbol titik
} koma dibelakang kondisi
while
while ( condition );

• statement akan diseksekusi kemudian while akan melakukan


pengecekan condition. Selama condition bernilai true,
maka statement akan dieksekusi berulang-ulang

Pemrograman Dasar 22
Konversi ke do-while
Program while disamping di konversi menjadi do-while
======
Scanner scan = new Scanner(System.in); Scanner scan = new Scanner(System.in);

String input = ""; String input = "";


while (!input.equalsIgnoreCase("quit")){ do {
System.out.print("Ketik ‘quit’ untuk System.out.print("Ketik ‘quit’ untuk
keluar:"); keluar:");
input = scan.next(); input = scan.next();
System.out.println(input); System.out.println(input);
} } while (!input.equalsIgnoreCase(“quit"));

Pemrograman Dasar 23
Perbedaan menggunakan while / do-while

Dengan "kondisi" yang bernilai false, perulangan do-while akan meng-


eksekusi statement didalamnya setidaknya satu kali

Pemrograman Dasar 24
Kombinasi Perulangan dna Percabangan

Pemrograman Dasar 25
Terima kasih

Pemrograman Dasar 26

Anda mungkin juga menyukai