Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

KONTROL ALUR EKSEKUSI PROGRAM BY : ZARNELLY,S.KOM,MSc.

Presentasi serupa


Presentasi berjudul: "KONTROL ALUR EKSEKUSI PROGRAM BY : ZARNELLY,S.KOM,MSc."— Transcript presentasi:

1 KONTROL ALUR EKSEKUSI PROGRAM BY : ZARNELLY,S.KOM,MSc

2 Kontrol Alur Eksekusi Kontrol Alur Eksekusi dibagi atas tiga bagian besar :  Seleksi  Looping  Jump

3 Seleksi Perintah Seleksi dalam java :  If  If..else  If jamak  switch

4 If (kondisi) statement; Syntax Pemakaian IF [grade >= 60] [grade < 60] print “Passed”

5 If (kondisi) statement-1; else statement-2; Syntax Pemakaian if..else [grade >= 60][grade < 60] print “Failed”print “Passed”

6 If (kondisi-1) statement-1; else if (kondisi-2) statement-2; else if (kondisi-3) statement-3; else default_statement Syntax Pemakaian if..else jamak

7 switch (ekpresi) { case nilai1: statement1; break; case nilai2: statement2; break; case nilai_n: statement_n; break; default : statementdefault; } Syntax Pemakaian switch

8 Contoh Program dengan IF jamak 1 // Fig. 2.20: Comparison.java 2 // Compare integers using if statements, relational operators 3 // and equality operators. 4 5 // Java packages 6 import javax.swing.JOptionPane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstNumber; // first string entered by user 14 String secondNumber; // second string entered by user 15 String result; // a string containing the output int number1; // first number to compare 18 int number2; // second number to compare // read first number from user as a string 21 firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); // read second number from user as a string 24 secondNumber = 25 JOptionPane.showInputDialog( "Enter second integer:" ); // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstNumber ); 29 number2 = Integer.parseInt( secondNumber ); 30 // initialize result to empty String 32 result = "";

9 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2; if ( number1 != number2 ) 38 result = result + number1 + " != " + number2; if ( number1 < number2 ) 41 result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) 44 result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) 47 result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) 50 result = result + "\n" + number1 + " >= " + number2; // Display results 53 JOptionPane.showMessageDialog( null, result, "Comparison Results", 54 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end method main } // end class Comparison

10

11 Contoh program dengan switch class TestSwitch2 { public static void main(String[] args) { int bulan = 4; switch(bulan) { case 1: System.out.println("pertama"); case 2: System.out.println("kedua"); case 3: System.out.println("kuartal 1"); break; case 4: case 5:

12 case 6: System.out.println("kuartal 2"); break; case 7: case 8: case 9: System.out.println("kuartal 3"); break; default: System.out.println("kuartal 4"); } System.out.println("keluar dari switch"); } Output kuartal 2 keluar dari switch

13 Looping Terdapat 3 jenis perintah looping :  While  Do..while  for

14 While Adalah jenis perulangan Benar, artinya perulangan akan dilakukan jika kondisi bernilai benar Syntax untuk statement tunggal while (kondisi) statement; Syntax untuk statement banyak while (kondisi) {blok statement; }

15 While [product <= 1000] [product > 1000] double product value merge decision Corresponding Java statement: product = 2 * product;

16 Contoh program dengan while 1 // Fig. 4.7: Average1.java 2 // Class-average program with counter-controlled repetition. 3 import javax.swing.JOptionPane; 4 5 public class Average1 { 6 7 public static void main( String args[] ) 8 { 9 int total; // sum of grades input by user 10 int gradeCounter; // number of grade to be entered next 11 int grade; // grade value 12 int average; // average of grades 13

17 14 String gradeString; // grade typed by user // initialization phase 17 total = 0; // initialize total 18 gradeCounter = 1; // initialize loop counter // processing phase 21 while ( gradeCounter <= 10 ) { // loop 10 times // prompt for input and read grade from user 24 gradeString = JOptionPane.showInputDialog( 25 "Enter integer grade: " ); // convert gradeString to int 28 grade = Integer.parseInt( gradeString ); 29

18 30 total = total + grade; // add grade to total 31 gradeCounter = gradeCounter + 1; // increment counter } // end while // termination phase 36 average = total / 10; // integer division // display average of exam grades 39 JOptionPane.showMessageDialog( null, "Class average is " + average, 40 "Class Average", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate the program } // end main } // end class Average1

19

20 Average1.java

21 Perulangan Do..While Pada perulangan ini penyeleksian kondisi dilakukan dibagian akhir sehingga blok statement akan diakses minimal satu kali baik dalam kondisi benar atau salah. Syntax do {blok statement; } while (kondisi);

22 Contoh do..while class TestIter2 { public static void main(String[] args) { int b = 9 ; System.out.println("Sebelum do"); do { System.out.println("Nilai b : "+b); b--; } while (b>=10); System.out.println("Setelah do"); } Output Sebelum do Nilai b : 9 Setelah do

23 Perulangan For Syntax For (inisialisasi;kondisi;iterasi) statement; Inisialisasi untuk menginisialisasi variabel yang menjadi pengontrol perulangan Kondisi pernyataan yang akan dieksekusi yang mengembalikan nilai benar atau salah Iterasi adalah pernyataan yang menjadi pengontrol jumlah perulangan Contoh : for(n=0;n<5;n++)

24 Contoh for class TestIter3 { public static void main(String[] args) { int c; System.out.println("Sebelum for"); for(c=0;c<5;c++) System.out.println("Nilai c: " + c); System.out.println("Setelah for"); } Output Sebelum for Nilai c: 0 Nilai c: 1 Nilai c: 2 Nilai c: 3 Nilai c: 4 Setelah for

25 Latihan Soal 1. Buat program dlm java menggunakan if atau case untuk menentukan apakah sebuah bilangan adalah ganjil atau genap 2. Buat program dlm java untuk mencetak deret , gunakan looping

26 Jawab 1 // Fig. 2.9: Addition.java // Addition program that displays the sum of two numbers. // Java packages import javax.swing.JOptionPane; // program uses JOptionPane public class GenapGanjil { // main method begins execution of Java application public static void main( String args[] ) { String bil; // first string entered by user int Genap; // first number to add int Ganjil; // second number to add // read in first number from user as a String bil = JOptionPane.showInputDialog( "masukkan bilangan" );

27 // convert numbers from type String to type int Genap = Integer.parseInt( bil ); // add numbers if (Genap%2== 0) // display result JOptionPane.showMessageDialog(null, + Genap +" bilangan Genap", "Results", JOptionPane.PLAIN_MESSAGE ); else JOptionPane.showMessageDialog(null, + Genap +" bilangan Ganjil", "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate application with window } // end method main } // end class Addition

28 Jawaban 2 // Addition program that displays the sum of two numbers. // Java packages import javax.swing.JOptionPane; // program uses JOptionPane public class Deret { // main method begins execution of Java application public static void main( String args[] ) { int bil=2; String Deret1=""; // add numbers while (bil<=10) { Deret1 = Deret1 + bil + " "; bil=bil+2; } // display result JOptionPane.showMessageDialog(null,Deret1, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate application with window } // end method main } // end class Addition


Download ppt "KONTROL ALUR EKSEKUSI PROGRAM BY : ZARNELLY,S.KOM,MSc."

Presentasi serupa


Iklan oleh Google