Looping While
Looping While
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.
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.
Pemrograman Dasar 8
Trace while Loop, cont.
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.
Pemrograman Dasar 11
Trace while Loop, cont.
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.
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 );
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);
Pemrograman Dasar 23
Perbedaan menggunakan while / do-while
Pemrograman Dasar 24
Kombinasi Perulangan dna Percabangan
Pemrograman Dasar 25
Terima kasih
Pemrograman Dasar 26