Judul TIU TIK Materi
Modul Perulangan Ganjil 2014/2015 Mahasiswa memahami Konsep Perulangan 1. Mahasiswa mampu menggunakan perintah perulangan For, While do, do While 2. Mahasiswa mampu menggunakan perintah perulangan for Loops A for loop is used to repeat a statement until a condition is met. Although for loops frequently are used for simple iteration in which a statement is repeated a certain number of times, for loops can be used for just about any kind of loop.
The start of the for loop has three parts: initialization is an expression that initializes the start of the loop. If you have a loop index, this expression might declare and initialize it, such as int i = 0. Variables that you declare in this part of the for loop are local to the loop itself; they cease to exist after the loop is finished executing. You can initialize more than one variable in this section by separating each expression with a comma. The statement int i = 0, int j = 10 in this section would declare the variables i and j, and both would be local to the loop. test is the test that occurs before each pass of the loop. The test must be a Boolean expression or a function that returns a boolean value, such as i < 10. If the test is true, the loop executes. When the test is false, the loop stops executing. increment is any expression or function call. Commonly, the increment is used to change the value of the loop index to bring the state of the loop closer to returning false and stopping the loop. The increment takes place after each pass of the loop. Similar to the initialization section, you can put more than one expression in this section by separating each expression with a comma. The statement part of the for loop is the statement that is executed each time the loop iterates. As with if, you can include either a single statement or a block statement. The previous example used a block because that is more common. The following example is a for loop that sets all slots of a String array to the value Mr.:
while and do Loops The remaining types of loops are while and do. As with for loops, while and do loops
1/5
Praktikum Alpro – Modul 3
enable a block of Java code to be executed repeatedly until a specific condition is met. Whether you use a for, while, or do loop is mostly a matter of your programming style.
while Loops The while loop repeats a statement for as long as a particular condition remains true.
do-while Loops The do loop is just like a while loop with one major difference—the place in the loop when the condition is tested. A while loop tests the condition before looping, so if the condition is false the first time it is tested, the body of the loop never executes. A do loop executes the body of the loop at least once before testing the condition, so if the condition is false the first time it is tested, the body of the loop already will have executed once. The following example uses a do loop to keep doubling the value of a long integer until it is larger than 3 trillion:
Breaking Out of Loops In all the loops, the loop ends when a tested condition is met. There might be times when something occurs during execution of a loop and you want to exit the loop early. In that case, you can use the break and continue keywords. The break keyword, it immediately halts execution of the current loop. If you have nested loops within loops, execution picks up with the next outer loop. Otherwise, the program simply continues executing the next statement after the loop. For example, recall the while loop that copied elements from an integer array into an array of floating-point numbers until either the end of the array or a 1 was reached. You can test for the latter case inside the body of the while loop and then use break to exit the loop:
2/5
Praktikum Alpro – Modul 3
Tugas Pendahuluan
1. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini * ** *** **** ***** ****** ******* Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n
2. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini 1 12 123 1234 12345 123456 1234567 Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n 3. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan
3/5
Praktikum Alpro – Modul 3
nilai n 4. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini 1 12 123 1234 12345 123456 1234567 123456 12345 1234 123 12 1 Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n 5. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini 1234567 123456 12345 1234 123 12 1 Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n 6. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini ******* ****** ***** **** *** ** * Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n 7. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini 1234567 123456
4/5
Praktikum Alpro – Modul 3
12345 1234 123 12 1 12 123 1234 12345 123456 1234567 Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n
Tugas Praktikum Waktu Praktikum Referensi
5/5
8. Buatlah flowchart/pseudocode dan program dalam java untuk menghasilkan output sebagaimana berikut ini ******* ****** ***** **** *** ** * ** *** **** ***** ****** ******* Output tersebut dihasilkan dari input n = 7. Output akan menyesuaikan berdasarkan nilai n Buat program dalam java untuk soal di atas (di tentukan oleh asisten, setiap kelompok berbeda) 2 x 50 menit 1. Java for Dummies, Barry Burd, Wiley Publishing, Inc, 2007 2. Sam Teach Yourselft, Java 6 in 21 Days, Rogers Cadenhead & Laura Lemay, SAMS, 2007
Praktikum Alpro – Modul 3