Flow of Control Araay Flow of Control Flow
- Slides: 32
Flow of Control & Araay
Flow of Control • Flow of Control mengacu pada sebuah komputer , agar dapat memproses sebuah pernyataan dalam sebuah progam. Flow of Control terbagi menjdi dua yaitu: • -Control Seleksi • -Control Perulangan 2
The Selection Statements • if-else • else-if • switch
if if (ekspresi boolean) { pernyataan 1; } pernyataan 2; if ekspresi = true Y N pernyataan 1 pernyataan 2
public class If { public static void main(String args[]) { int bilangan = -5; if (bilangan<0) System. out. println(“Bilangan adalah negatif”); } }
if-else if (ekspresi boolean) { pernyataan 1; } else { pernyataan 2; } pernyataan 3; Y if ekspresi = true pernyataan 1 N pernyataan 2 pernyataan 3
public class If. Else { public static void main(String args[]) { int bilangan = -5; if (bilangan<0) System. out. println(“Bilangan adalah negatif”); else System. out. println(“Bilangan adalah positif”); } }
else-if if (ekspresi boolean 1) { pernyataan 1; } else if (ekspresi boolean 2) { pernyataan 2; } else { pernyataan 3; } pernyataan 4; if ekspresi 1 = true N Y Y if ekspresi 2 = true pernyataan 2 N pernyataan 3 pernyataan 4 pernyataan 1
switch (ekspresi) { case konstanta 1 : pernyataan 1; break; case konstanta 1: pernyataan 2; break; default : pernyataan 3; } pernyataan 4; if N ekspresi = konstan 1 Y if Y N ekspresi = konstan 2 pernyataan 1 N pernyataan 2 pernyataan 3 if break Y if break N pernyataan 4 Y
switch(x) • Variabel x harus bertipe byte, short, char, atau int. • Floating point, long, atau class references (termasuk String) tidak diperbolehkan. • Kedudukan statement pada default sama dengan kedudukan else pada if-else.
public class Switch { public static void main(String args[]) { int i=2; switch (i) { case 1 : i+=3; break; case 2 : i+=5; break; default: i+=10; } System. out. println(i); } }
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 14
Repetition Constructs • Bentuk perintah perulangan dalam Java: – for Loop – while Loop – do-while Loop 15
While Loop • Bentuk umum: Jika ekspresi boolean bernilai true, the loop body is dijalankan. a variable is initialized <initialisation> while <testing> { <Loop Body Statements> <update> } <program continues here> the loop body is executed again, etc… the variable is tested the value of the variable is updated 16
Example – While Loop <initialization> int num = 0; <testing> while (num < 10) { System. out. println("The value of num is " + num); loop body num = num + 1; } System. out. println("The End"); <update> What is the output of this program fragment? 17
Counter Controlled loop int num = 0; counter while (num < 10) { System. out. println("Counting " + num); num++; } System. out. println("The End"); 18
Sentinel-controlled loop System. out. print("Do you understand? "); Scanner keyboard = new Scanner (System. in); <initialisation> char answer; answer = keyboard. next(). char. At(0); loop body while (answer == 'N' || answer == 'n') { System. out. println("I will explain again"); System. out. println("blah. . "); System. out. print("NOW do you understand? "); <testing against sentinel> answer = keyboard. next(). char. At(0); } System. out. println("Good!"); <update> 19
For Loops • A for loop dalah sebuah cara yang simpel untuk menulis Counter Controler loop. • Ini ada beberapa bagian: – <initialisation> – <testing> – <update> • for loop: for (<initialisation>; <testing>; <update>) { // loop. . ? } 20
Example • Bandingkan loop while- dan for- di bawah ini: int i = 1; while (i <= 5) { System. out. println(i); i++; } initialisation testing update for(int i = 1; i <= 5; i++) System. out. println(i); 21
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 Scanner = 1 + 2 + =3 new + Scanner 4 + …(System. in); + n keyboard int sum = 0; System. out. print("what is the limit? "); int limit = keyboard. next. Int(); 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"); 22
The do-while loop • Yang ketiga di repetition construct dalam Java adalahdo-while loop. • ini <testing> dilakuukan setelah • Syntax: <initialisation> do { // loop body <update> } while <testing> 23
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. next. Int(); 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. next. Int(); System. out. print("The square root of " + count + " is "); System. out. println(Math. sqrt(count)); count++; } while (count < size); 24
Output The output for while loop Enter an integer: -5 Press any key to continue. . . The output for do_while loop Enter an integer -5 The square root of 0 is 0. 0 Press any key to continue. . . 25
Defenisi Array • Array adalah sebuah variabel/sebuah lokasi tertentu yang memiliki satu nama sebagai identifier, namun identifier ini dapat menyimpan lebih dari sebuah nilai.
Deklarasi Array • Deklarasi Array int []ages; atau sepasang tanda kurung [] sesudah nama identifier. int ages[]; //deklarasi int ages[]; //instantiate obyek ages = new int[100]; atau bisa juga ditulis dengan, //deklarasi dan instantiate Obyek : int ages[] = new int[100]; Pada contoh diatas, pendeklarasian tersebut akan memberitahukan kepada compiler Java, bahwa identifier ages akan digunakan sebagai nama array yang berisi data bertipe integer, dan dilanjutkan dengan membuat atau meng -instantiate sebuah array baru yang terdiri dari 100 elemen.
Deklarasi Array • Anda juga dapat mendeklarasikan, membangun, kemudian memberikan sebuah nilai pada array sekaligus dalam sebuah pernyataan. Sebagai contoh: //membuat sebuah array yang berisi variabel //boolean pada sebuah identifier. Array ini terdiri dari 4 //elemen yang diinisilisasikan sebagai value //{true, false, true, false} boolean results[] ={ true, false, true, false };
Deklarasi Array //Membuat sebuah array yang terdiri dari penginisialisasian //4 variabel double bagi value {100, 90, 80, 75} double []grades = {100, 90, 80, 75}; //Membuat sebuah array String dengan identifier days. Array //ini terdiri dari 7 elemen. String days[] = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”};
Mengakses Array • Untuk mengakses sebuah elemen dalam array, melalui : Angka no indeks atau subscript (selalu integer). • indeks array dimulai dari 0 s/d (ukuran. Array-1). • Contoh : Untuk ages = new int[100] //memberikan nilai 10 kepada elemen pertama array ages[0] = 10; //mencetak elemen array yang terakhir System. out. print(ages[99]);
Contoh public class Array. Sample{ public static void main( String[] args ){ int[] ages = new int[100]; for( int i=0; i<100; i++ ){ System. out. print( ages[i] ); }}} • contoh kode untuk mencetak seluruh elemen menggunakan atribut length dari array. ini akan mengembalikan ukuran didalam array. Atribut dari array itu sendiri. Sebagai contoh, mengunakanla h pernyataan for loop, supaya kode menjadi lebih pendek. array. Name. length Pada contoh sebelumnya, kita dapat menuliskannya kembali seperti berikut ini, public class Array. Sample { public static void main( String[] args ){ int[] ages = new int[100]; for( int i=0; i<ages. length; i++ ){ System. out. print( ages[i] ); } } }
Contoh : public class Array. Multi { public static void main(String[] arg) { String [][]mhs={{"123", "Budi Susanto", "Jakarta"}, {"124", "Geni Handayani", "Surabaya"}}; // get value of elements for(int i=0; i<2; i++){ for(int j=0; j<3; j++){ System. out. println(mhs[i][j]); } System. out. println(); } } }
- Data flow vs control flow
- Control flow and data flow computers
- Transaction flow graph
- Stock control e flow control
- Flow control and error control
- Magic box respiratory
- Trach collar oxygen flow rates
- T piece ventilation
- Turbulent flow definition
- Internal flow vs external flow
- Energy naturally flows from warmer matter to cooler matter.
- Oikos meaning
- Dfd to structure chart
- Data flow structure
- Define irrotational flow
- Internal flow and external flow
- Cheddar cheese flowchart
- Picv valve symbol
- Udp flow control
- Je instruction in 8086
- Exceptional control flow
- Repl capture/apply: flow control
- Material flow control
- Control flow graph for bubble sort
- Fluid mechanics symbols
- Unity flow chart
- Database security definition
- Trim valves
- Blood flow control
- Control hijacking
- Ipdom
- Dd path graph is called
- Throttle valve symbol