Dasar-Dasar Pemrograman Nested Loop & Rekursif Marsel Willem Aipassa, S. Kom. Dasar-Dasar Pemrograman
Nested Loop Logical structure used in computer programming where two repeating statements are placed in a "nested" form. In a nested loop, the first iteration of the outer loop causes the inner loop to execute. The inner loop then repeats for as many times as is specified. When the inner loop completes, the outer loop is executed for its second iteration, triggering the inner loop again, and so on until the requirements for the outer loop are complete. Marsel Willem Aipassa, S. Kom.
Example (Multiply Table) N\M 1 2 3 4 5 6 7 8 10 12 14 16 9 13 15 18 21 24 20 28 32 25 30 35 40 36 42 48 for loop1 := 1 to 6 do begin for loop2 := 1 to 8 do begin write(loop1*loop2,' '); end; writeln(); Marsel Willem Aipassa, S. Kom.
Latihan Membuat aplikasi yang akan memberikan output seperti berikut: 1 2 3 4 5 … n ……… n kali Marsel Willem Aipassa, S. Kom.
Tugas Kelas 1 2 3 4 5 … n ……… n kali Buatlah program yang dapat memberikan output berikut berdasarkan inputan n. 1 2 3 4 5 … n ……… n kali Marsel Willem Aipassa, S. Kom.
Recursion function a(nilai : integer) begin a(3); end; Method of defining functions in which the function being defined is applied within its own definition. Recursive are typically slower than iterative functions. Recursive must have a condition to stop. Not every programming language allows recursion, because it can cause lack of memory. function a(nilai : integer) begin a(3); end; Marsel Willem Aipassa, S. Kom.
Contoh (Penambahan bilangan) Membuat aplikasi untuk menghitung dengan aturan: f(n) = f(n)+f(n-1)+f(n-2)+f(n-3) +f(n-4) Contoh: f(5) = 5+4+3+2 +1 = 15 Marsel Willem Aipassa, S. Kom.
When should we use iteration, and when use recursion? There are (at least) these three factors to consider: Iterative functions are typically faster than their recursive counterparts. So, if speed is an issue, you would normally use iteration. If the stack limit is too constraining then you will prefer iteration over recursion. Some procedures are very naturally programmed recursively, and all but unmanageable iteratively. Marsel Willem Aipassa, S. Kom.
Tugas Buatlah aplikasi untuk menghitung nilai faktorial dari suatu bilangan. Dalam perhitungan tidak diperbolehkan menggunakan operator perkalian, hanya dengan operator penambahan dengan menggunakan Nested Looping! Contoh : input = 4 output = 24 (faktorial dari 4) Buatlah aplikasi untuk menampilkan deret fibonaci. Tidak diperbolehkan menggunakan rumus. Gunakan Metode Rekursif. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597….. Kumpulkan besok jam 2 di lab E201A Marsel Willem Aipassa, S. Kom.