PERULANGANPERULANGAN. 2 Flow of Control Flow of Control refers to the order that the computer processes the statements in a program. –Sequentially; baris.

Slides:



Advertisements
Presentasi serupa
Array.
Advertisements

+ Pemrograman Javascript Teknik Informatika Universitas Bunda Mulia Jakarta Chandra Hermawan H., M.Kom., MM.
3. Economic Returns to Land Resources: Theories of Land Rent
Mata Kuliah : ALGORITMA dan STRUKTUR DATA 1.
PEMOGRAMAN BERBASIS JARINGAN
ESTIMASI PENJUALAN DATA TIME SERIES - DEKOMPOSISI 1. ADDITIVE MODEL 2. MULTIPLICATIVE MODEL.
Peta Kontrol (Untuk Data Variabel)
Slide 3-1 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Revised by IB & SAM, Fasilkom UI, 2005 Exercises Apa saja komponen utama.
Prinsip Pembuatan Study Guide
Introduction to The Design & Analysis of Algorithms
Penerapan Fungsi Non-Linier
PENGENALAN PL/SQL.
PROSES PADA WINDOWS Pratikum SO. Introduksi Proses 1.Program yang sedang dalam keadaan dieksekusi. 2.Unit kerja terkecil yang secara individu memiliki.
Review Operasi Matriks
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. A4: Bahasa Rakitan AVR Conditional & Branch Instructions 21 Maret 2003 Bobby Nazief
DEPARTEMEN ILMU KOMPUTER FMIPA IPB 2011 Praktikum Bahasa Pemrograman.
Interface Nur Hayatin, S.ST Jurusan Teknik Informatika Universitas Muhammadiyah Malang Sem Genap 2010.
Bilqis1 Pertemuan bilqis2 Sequences and Summations Deret (urutan) dan Penjumlahan.
Menjelaskan sifat – sifat komponen elektronika aktif dan pasif
PROGRAM AND MATERIALS DEVELOPMENT in ODL. How to design and develop the ODL programs and courses? Use systematic and systemic design know well the students.
JAVA CLASS Bahasa Pemrogramam BAHASA PEMROGRAMAN PERTEMUAN #9.
VALUING COMMON STOCKS Expected return : the percentage yield that an investor forecasts from a specific investment over a set period of time. Sometimes.
Implementing an REA Model in a Relational Database
Algoritma : CONTROL STRUCTURES
Universitas Muhammadiyah Malang Fakultas Teknik Jurusan Teknik Informatika Semester Genap Nur hayatin, S.ST.
MEMORY Bhakti Yudho Suprapto,MT. berfungsi untuk memuat program dan juga sebagai tempat untuk menampung hasil proses bersifat volatile yang berarti bahwa.
SUBPROGRAM IN PASCAL Function.
Basisdata Pertanian. After completing this lesson, you should be able to do the following Identify the available group functions Describe the use of group.
Array
2nd MEETING Assignment 4A “Exploring Grids” Assignment 4 B “Redesign Grids” Create several alternatives grid sysytem using the provided elements: (min.
Slide 1 QUIS Langkah pertama caranya Buat di slide pertama judul Slide kedua soal Slide ketiga waktu habis Slide keempat jawaban yang benar Slide kelima.
Lecture 1 Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Linked List dan Double Linked List
Amortization & Depresiasi
SMPN 2 DEMAK GRADE 7 SEMESTER 2
Looping (perulangan) As’ad Djamalilleil
STRUCTURAL CONTROL continuation STATEMENT  SWITCH  WHILE  DO..WHILE.
Operator dan Assignment Pertemuan 3 Pemrograman Berbasis Obyek Oleh Tita Karlita.
1. 2 Work is defined to be the product of the magnitude of the displacement times the component of the force parallel to the displacement W = F ║ d F.
Pemrograman JAVA (TIB09)
PERTEMUAN 4 Penyeleksian kondisi
Double Linked List. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Double Linked List Sama seperti single linked list, double.
TCP, THREE-WAY HANDSHAKE, WINDOW
Struktur Kontrol #2 Perulangan
Pengenalan Pemrograman 1 Versi 2.0 Struktur kontrol.
Web Teknologi I (MKB511C) Minggu 12 Page 1 MINGGU 12 Web Teknologi I (MKB511C) Pokok Bahasan: – Text processing perl-compatible regular expression/PCRE.
Control Structures in Java Risanuri Hidayat, Ir., M.Sc.
MINGGU 2 Java Programming (MKB614C)
PEMROGRAMAN BERORIENTASI OBJEK
Nandang Hermanto PK2 Pertemuan 3. Perulangan Pernyataan while Pernyataan do..while Pernyataan for.
2. Introduction to Algorithm and Programming
Pengenalan Pemrograman 1 Versi 2.0 Struktur kontrol.
Algoritme dan Pemrograman
PENDALAMAN LOOP DAN LOGIKA
Tenia Wahyuningrum.  Struktur kontrol pengulangan Digunakan untuk menjalankan satu atau beberapa pernyataan sebanyak beberapa kali.  Tipe: Pengulangan-while.
Tenia Wahyuningrum.  Struktur kontrol pengulangan Digunakan untuk menjalankan satu atau beberapa pernyataan sebanyak beberapa kali.  Tipe: Pengulangan-while.
PERNYATAAN PERULANGAN
Perulangan (Iteration)
While … do … Repeat … until … For … to … do …
Perulangan.
Konsep pemrograman LOOP
PERULANGAN WHILE.
Pemrograman Terstruktur
Struktur Kontrol Struktur kontrol merupakan pengatur aliran program
Array Buat algoritma untuk mencari nilai terbesar dari 5 nilai mahasiswa yang diinputkan dengan array.
Perulangan.
Struktur Data dan Algoritma
Algoritma & Pemrograman 1 Achmad Fitro The Power of PowerPoint – thepopp.com Chapter 4.
Sorting using a Lambda Expression
CMP 131 Introduction to Computer Programming
Transcript presentasi:

PERULANGANPERULANGAN

2 Flow of Control Flow of Control refers to the order that the computer processes the statements in a program. –Sequentially; baris per baris –Conditionally; dengan perintah percabangan –Repetitively; memakai perulangan Today we will consider repetition constructs in this lecture.

3 Repetisi/ Perulangan Repetisi dalam program komputer terjadi ketika kita ingin menjalankan kembali beberapa perintah: Contoh.1) Score for student 1: 80 Score for student 2: 92 Score for student 3: 45 Score for student 4: 56 Score for student 5: 60 Average Score: 66.6

4 Repetition Constructs Bentuk perintah perulangan dalam Java: –for Loop –while Loop –do-while Loop Which one to use depending on: –when the loop should start/ kapan dimulai –when the loop should stop/ kapan berakhir –what kind of action the loop body will take/ tindakan apa yang akan dilakukan

5 While Loop Bentuk umum: while { } a variable is initialized the variable is tested Jika ekspresi boolean bernilai true, the loop body is dijalankan. the value of the variable is updated the loop body is executed again, etc…

6 Example – While Loop int num = 0; while (num < 10) { System.out.println("The value of num is " + num); num = num + 1; } System.out.println("The End"); loop body What is the output of this program fragment?

7 Pemberian nilai diri sendiri The statement num = num + 1; means to increase the value of num by 1. (The expression on the right of the equals sign is evaluated and then assigned to the identifier on the left.) A simpler way to write this is: num++; We can also write: total = total + num as total += num

8 Exercise/ Latihan Apakah output dari program ini jika nilai limit: a)3 b)10 c)0 Scanner keyboard = new Scanner (System.in); System.out.print ("Limit? "); int limit; limit = keyboard.nextInt(); int i = 1; while (i <= limit) { System.out.println(i); i++; }

9 Buatlah program Java yang meminta masukan jumlah data nilai yang akan dihitung. Kemudian di akhir program akan menampilkan jumlah total dari nilai-nilai yang dimasukkan tersebut. You will need variables to store: –score of assignment –total score –counter Modify the program to calculate the average of the assignment scores. Menghitung Nilai Total

10 Counter Controlled loop int num = 0; while (num < 10) { System.out.println("Counting " + num); num++; } System.out.println("The End"); counter

11 Sentinel-controlled loop System.out.print("Do you understand?"); Scanner keyboard = new Scanner (System.in); char answer; answer = keyboard.next().charAt(0); while (answer == 'N' || answer == 'n') { System.out.println("I will explain again"); System.out.println("blah blah blah.."); System.out.print("NOW do you understand?"); answer = keyboard.next().charAt(0); } System.out.println("Good!"); loop body

12 counter vs sentinel controlled while loop Counter-controlled: Write a Java program that asks the user how many numbers are required, then lets the user input the value of each number then display the total. How many numbers? 3 Enter number: 12 Enter number: 10 Enter number: 5 Sum of 3 numbers: 27 Enter number (negative to stop): 12 Enter number (negative to stop): 10 Enter number (negative to stop): 5 Enter number (negative to stop): -5 Sum of 3 numbers: 27 Sentinel-controlled: Write a Java program that asks the user to enter numbers and displays the total of all the numbers. To stop, the user has to enter a negative number.

13 Infinite loops What happens if the counter value is not updated? Scanner keyboard = new Scanner (System.in); int sum = 0; int counter = 1; int num; while (counter < 5) { System.out.print("Enter number : "); num = keyboard.nextInt(); sum += num; } System.out.println("The sum is " + sum);

14 Tugas Individu 1.Buat website/ blog (kecuali yang sudah punya). 2.Tambahkan link di blog Anda ke Buat resume tentang: NPM ganjil : perintah for NPM genap : perintah do-while Berisi : 1 halaman teori, 1 halaman contoh program, output dan pembahasan/tracing-nya. 4.Posting resume Anda ke blog masing-masing. 5.Tuliskan alamat blog Anda di bagian komentar artikel “Tugas Loop BP” di blog Paling lambat 24 Oktober 2010 pukul WIB

15 For Loops A for loop is a simpler way of writing a counter-controlled loop. It also consists of the three parts: – Syntax for a for loop: for ( ; ; ) { // loop body goes here }

16 Example Bandingkan loop while - dan for - di bawah ini: int i = 1; while (i <= 5) { System.out.println(i); i++; } for(int i = 1; i <= 5; i++) System.out.println(i); initialisation testing update

17 Exercise/ Latihan Write a Java program that finds the square roots of the numbers 100, 110, 120, … 200. Hint: –what is the initialisation? –What is the update? –What will you be testing? –What's in the loop body? The for loop should be used when it is clearly a counter-controlled loop.

18 Example Write a Java program that finds the sum of the integer numbers 1 to n, where n is a value entered by the user. sum = … + n Scanner keyboard = new Scanner (System.in); int sum = 0; System.out.print("what is the limit?"); int limit = keyboard.nextInt(); for (int i = 1; i <= limit; i++) { sum += i; } if (limit > 0) System.out.println("The sum is " + sum); else System.out.println("You must enter a positive number");

19 Exercise Write a Java program that asks the user for a limit, n, and then calculates the sum: –sum = … + n How about –sum = … + n 2

20 The do-while loop The third kind of repetition construct in Java is the do-while loop. It is similar to the while loop but the is done after the loop body This means the loop body is executed at least once. Syntax: do { // loop body } while

21 While vs. Do-while What is the output if size = -5? int count = 0; Scanner keyboard = new Scanner (System.in); System.out.println("Enter an integer:"); int size = keyboard.nextInt(); while (count < size) { System.out.print("The square root of " + count + " is "); System.out.println(Math.sqrt(count)); count++; } int count = 0; int size; do { System.out.println("Enter an integer"); Scanner keyboard = new Scanner (System.in); size = keyboard.nextInt(); System.out.print("The square root of " + count + " is "); System.out.println(Math.sqrt(count)); count++; } while (count < size);

22 The output for do_while loop Enter an integer -5 The square root of 0 is 0.0 Press any key to continue... The output for while loop Enter an integer: -5 Press any key to continue... Output

23 Exercise Modify the following program to use a do-while loop. char answer; System.out.print("Are you (m)ale or (f)emale?"); Scanner keyboard = new Scanner (System.in); answer = keyboard.next().charAt(0); while ((answer != 'm') && (answer != 'f')) { System.out.println("Are you (m)ale or (f)emale?"); System.out.println("Please enter m or f"); answer = keyboard.next().charAt(0); } System.out.println("Good!");

24 Menus Because do-while loops will execute the loop body at least once, they are useful for creating menus: Welcome to Riches Bank! What do you want to do today? 1.Create a new account 2.Make a deposit 3.Make a withdrawal 4.Check your balance 5.Quit Enter Choice:

25 Write a Java program that asks the user to enter a double value, then provides the user with a list of choices: –to calculate the square of the value, n 2 –to calculate the cube of the value, n 3 –to calculate the square root of the value,  n –to calculate the reciprocal of the value, 1/n –quit. Modify the program to ask the user if they want to enter another number and allow them to do so. Exercise