Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
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
Syntax Pemakaian IF If (kondisi) statement; [grade >= 60]
print “Passed”
5
Syntax Pemakaian if..else
If (kondisi) statement-1; else statement-2; [grade >= 60] [grade < 60] print “Failed” print “Passed”
6
Syntax Pemakaian if..else jamak
If (kondisi-1) statement-1; else if (kondisi-2) statement-2; else if (kondisi-3) statement-3; else default_statement
7
Syntax Pemakaian switch
switch (ekpresi) { case nilai1: statement1; break; case nilai2: statement2; case nilai_n: statement_n; default : statementdefault; }
8
Contoh Program dengan IF jamak
// Fig. 2.20: Comparison.java // Compare integers using if statements, relational operators // and equality operators. 4 // Java packages import javax.swing.JOptionPane; 7 public class Comparison { 9 // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // first string entered by user String secondNumber; // second string entered by user String result; // a string containing the output 16 int number1; // first number to compare int number2; // second number to compare 19 // read first number from user as a string firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); 22 // read second number from user as a string secondNumber = JOptionPane.showInputDialog( "Enter second integer:" ); 26 // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); // initialize result to empty String result = "";
9
if ( number1 == number2 ) result = result + number1 + " == " + number2; 36 if ( number1 != number2 ) result = result + number1 + " != " + number2; 39 if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; 42 if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; 45 if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; 48 if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; 51 // Display results JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); 55 System.exit( 0 ); // terminate application 57 } // end method main 59 60 } // end class Comparison
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
Output kuartal 2 keluar dari switch case 6:
System.out.println("kuartal 2"); break; case 7: case 8: case 9: System.out.println("kuartal 3"); 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 { blok statement; }
15
While merge decision [product <= 1000] double product value
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 public static void main( String args[] ) { int total; // sum of grades input by user int gradeCounter; // number of grade to be entered next int grade; // grade value int average; // average of grades 13
17
14 String gradeString; // grade typed by user
15 // initialization phase total = 0; // initialize total gradeCounter = 1; // initialize loop counter 19 // processing phase while ( gradeCounter <= 10 ) { // loop 10 times 22 // prompt for input and read grade from user 24 gradeString = JOptionPane.showInputDialog( "Enter integer grade: " ); 26 // convert gradeString to int grade = Integer.parseInt( gradeString ); 29
18
30 total = total + grade; // add grade to total
31 gradeCounter = gradeCounter + 1; // increment counter 32 } // end while 34 // termination phase average = total / 10; // integer division 37 // display average of exam grades JOptionPane.showMessageDialog( null, "Class average is " + average, "Class Average", JOptionPane.INFORMATION_MESSAGE ); 41 System.exit( 0 ); // terminate the program 43 } // end main 45 46 } // end class Average1
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 Buat program dlm java menggunakan if atau case untuk menentukan apakah sebuah bilangan adalah ganjil atau genap 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", 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 Jawaban 2
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.