Control Structures in Java Risanuri Hidayat, Ir., M.Sc.

Slides:



Advertisements
Presentasi serupa
Malang,22 November 2012
Advertisements

 public static void main(String[] args) {  int bil1=3;  do {  System.out.print(bil1+",");  bil1=bil1+4;  }  while(bil1
Dasar Pemrograman Komputer
Algoritma : CONTROL STRUCTURES
Universitas Muhammadiyah Malang Fakultas Teknik Jurusan Teknik Informatika Semester Genap Nur hayatin, S.ST.
Macam Statement As’ad Djamalilleil
Selection (pemilihan) As’ad Djamalilleil
Pemrograman Berorientasi Objek
Modul 3: Kendali program dan teknik penyimpanan data
Flow Control & Exception Handling
Pemrograman JAVA (TIB09)
Struktur Kontrol.
Pengenalan Pemrograman 1 Versi 2.0 Struktur kontrol.
KONTROL ALUR EKSEKUSI PROGRAM
PERTEMUAN 2 Variabel, Ekspresi, Operator, dan Flow Control
Struktur Kendali Proses (Seleksi)
MINGGU 2 Java Programming (MKB614C)
PEMROGRAMAN KOMPUTER 2 4 SKS 2 TEORI DAN 2 PRAKTEK
PEMROGRAMAN BERORIENTASI OBJEK
LOGIKA ALGORITMA Pertemuan 6.
MATERI 8 ALGORITMA DAN PEMROGRAMAN
Pemrograman Berorientasi Objek Bab 1 – Pemrograman Terstruktur.
Struktur Kontrol.
Nandang Hermanto PK2 Pertemuan 3. Perulangan Pernyataan while Pernyataan do..while Pernyataan for.
Flow Control Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2008.
Pengenalan Pemrograman 1 Versi 2.0 Struktur kontrol.
- PERTEMUAN 9 - BERBAGAI KELAS UTILITAS DI JAVA
PENDALAMAN LOOP DAN LOGIKA
- PERTEMUAN 4 - PERULANGAN
Flow Control & Looping Pertemuan 4 Pemrograman Berbasis Obyek Oleh Tita Karlita.
PELATIHAN JAVA FUNDAMENTAL
Diberikan pada Kuliah Sela Teknik Informatika - Universitas Muhammadiyah Malang Tahun 2011.
Struktur Kendali Proses (Perulangan)
Struktur Kontrol Pemilihan
Struktur kontrol ∞ kondisi. ∞ if-else ,
Public class RelasiDemo { public static void main(String[] args) { //beberapa nilai int i = 37; int j = 42; int k = 42; System.out.println("Nilai variabel...");
Struktur kontrol.
PERNYATAAN PERULANGAN
Perulangan (Iteration)
03 Elemen Dasar Bahasa Java
Object-oriented Programming (OOP) with JAVA 2011/2012
Struktur Kontrol Struktur kontrol if Struktur kontrol if-else
If, If/else, switch SUSSI.
Pernyataan Pertemuan 3 Season 1
PERNYATAAN PERULANGAN
STRUKTUR KONTROL.
PENGENDALI PROGRAM 1 Pokok bahasan :
Konsep pemrograman LOOP
INPUT DATA, PERCABANGAN & PERULANGAN DENGAN JAVA
PERCABANGAN & PERULANGAN DALAM JAVA
Pertemuan ke 10 Perintah Bercabang.
BAB 4 Flow Control & Looping
Aliran Kendali (Flow Control)
Bifurcation & Lompatan Instruksi
Perulangan / Looping / Repetisi PEMROGRAMAN DASAR
Percabangan dan Perulangan.
Algoritma & Pemrograman 3
PENERAPAN DASAR-DASAR SYNTAX JAVA
Latihan & Studi Kasus Perulangan / Looping PEMROGRAMAN DASAR
JAVA FUNDAMENTAL.
Bifurcation & Lompatan Instruksi
JAVA FUNDAMENTAL.
Bahasa Pemrograman (Pemrograman Visual)
Percabangan/Penyeleksian Kondisi
PEMrograman web database
Perulangan Pernyataan while Pernyataan do..while Pernyataan for.
Pernyataan Kondisional, Perulangan dan Jump
Konsep Bahasa Pemrograman I Operator
Logika dan Algoritma Agung BP Chapter 5.
Struktur Kendali MINGGU KE-2.
Transcript presentasi:

Control Structures in Java Risanuri Hidayat, Ir., M.Sc.

Condition OperatorMeaningExample ==Equal tocount == 10 !=Not equal toflag != DONE <Less thana < b <=Less than or equal to<= LIMIT >Greater thanpointer > end_of_list >=Greater than or equal to lap >= start

Statement Kendali Digunakan untuk mengatur aliran perintah program dan percabangan. Terdiri atas : seleksi, iterasi, dan lompatan Selection memungkinkan program mengalir ke jalur yangberbeda berdasarkan pada keluaran ekspresi yang dihasilkan. Iteration memungkinkan suatu eksekusi program berulang kembali. Jump memungkinkan suatu program melompat ke eksekusi tertentu.

Selection If if (condition) statement1; else statement2; int a, b; //... if(a < b) a = 0; else b = 0;

Nested if nested if adalah statement if yang targetnya adalahjuga if atau else. if (i == 10) { if (j < 20) a = b; if (k > 100) c = d; // this if is else a = c; // associated with this else } else a = d; // this else refers to if(i == 10)

if-else-if Ladder Bentuknya: if(condition) statement; else if(condition) statement; else if(condition) statement;. else statement;

if-else-if Ladder // Demonstrate if-else-if statements. public class IfElse { public static void main(String args[]) { int month = 4; // April String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); }

switch switch merupakan statement percabangan dengan banyak cabang. Bentuknya seperti berikut: switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break;. case valueN: // statement sequence break; default: // default statement sequence }

switch expression harus bertype byte, short, int, or char; // A simple example of the switch. public class SampleSwitch { public static void main(String args[]) { for(int i=0; i<6; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break;

switch case 2: System.out.println("i is two."); break; case 3: System.out.println("i is three."); break; default: System.out.println("i is greater than 3."); } // switch } // main } // class

Nested switch Kita dapat juga membuat statement switch di dalam switch yang lain switch(count) { case 1: switch(target) { // nested switch case 0: System.out.println("target is zero"); break; case 1: // no conflicts with outer switch System.out.println("target is one"); break; } // switch(target) break; case 2: //...

Iteration while while loop merupakan dasar looping di Java. While akan mengulang statement jika kondisi yang disyaratkan benar. Bentuk statement while adalah: while(condition) { // body of loop }

while // Demonstrate the while loop. public class While { public static void main(String args[]) { int n = 10; while(n > 0) { System.out.println("tick " + n); n--; } // while } // main } // class

do-while Sering kali dalam program kita membuat instruksi terlebih dahulu baru kemudian di-test hasilnya. Hal ini juga sering terjadi dalam looping. Java mm-fasilitasi hal ini dengan do- while. Bentuknya sebagai berikut: do { // body of loop } while (condition);

do-while // Demonstrate the do-while loop. public class DoWhile { public static void main(String args[]) { int n = 10; do { System.out.println("tick " + n); n—; } while(n > 0); } // main } // class

for For merupakan statement loop yang paling sering digunakan dalam berbagai bahasa, termasuk Java. Berikut ini bentuk umumnya: for(initialization; condition; iteration) { // body }

for // Demonstrate the for loop. public class ForTick { public static void main(String args[]) { int n; for(n=10; n>0; n—) System.out.println("tick " + n); } public class ForTick { public static void main(String args[]) { for(int n=10; n>0; n—) System.out.println("tick " + n); }

for // Using the comma. class Comma { public static void main(String args[]) { int a, b; for(a=1, b=4; a<b; a++, b—) { System.out.println("a = " + a); System.out.println("b = " + b); }

Nested Loops Like all other programming languages, Java allows loops to be nested. That is, one loop may be inside another. For example, here is a program that nests for loops: // Loops may be nested. class Nested { public static void main(String args[]) { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) System.out.print("."); System.out.println(); }

Jump Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program.

break In Java, the break statement has three uses. First, as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop. Third, it can be used as a "civilized" form of goto.

break // Using break to exit a loop. class BreakLoop { public static void main(String args[]) { for(int i=0; i<100; i++) { if(i == 10) break; // terminate loop if i is 10 System.out.println("i: " + i); } System.out.println("Loop complete."); }

break // Using break to exit a while loop. class BreakLoop2 { public static void main(String args[]) { int i = 0; while(i < 100) { if(i == 10) break; // terminate loop if i is 10 System.out.println("i: " + i); i++; } System.out.println("Loop complete."); }

break loops, the break statement can also be employed by itself to provide a "civilized" form of the goto statement. The general form of the labeled break statement is shown here: break label;

break // Using break as a civilized form of goto. class Break { public static void main(String args[]) { boolean t = true; first: { second: { third: { System.out.println("Before the break."); if(t) break second; // break out of second block System.out.println("This won't execute"); } //third System.out.println("This won't execute"); } // second System.out.println("This is after second block."); } // first } // main } // class

continue you might want to continue running the loop, but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop's end. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

continue // Demonstrate continue. class Continue { public static void main(String args[]) { for(int i=0; i<10; i++) { System.out.print(i + " "); if (i%2 == 0) continue; System.out.println(""); }

continue As with the break statement, continue may specify a label to describe which enclosing loop to continue.

continue // Using continue with a label. class ContinueLabel { public static void main(String args[]) { outer: for (int i=0; i<10; i++) { for(int j=0; j<10; j++) { if(j > i) { System.out.println(); continue outer; } System.out.print(" " + (i * j)); } System.out.println(); }

return The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. The following example illustrates this point. Here, return causes execution to return to the Java run- time system, since it is the run-time system that calls main( ).

return // Demonstrate return. class Return { public static void main(String args[]) { boolean t = true; System.out.println("Before the return."); if(t) return; // return to caller System.out.println("This won't execute."); }