Flow Control Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2008
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS The Selection Statements if if-else else-if switch
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS if if (ekspresi boolean) { pernyataan1; } pernyataan2; if ekspresi = true pernyataan1 pernyataan2 Y N
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS public class If { public static void main(String args[]) { int bilangan = -5; if (bilangan<0) System.out.println(“Bilangan adalah negatif”); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS if-else if (ekspresi boolean) { pernyataan1; } else { pernyataan2; } pernyataan3; if ekspresi = true pernyataan2 pernyataan3 YN pernyataan1
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS If - else If() statement takes a boolean expression, not a numeric value. You cannot convert or cast boolean types and numeric types. If you have: if (x) // x is int use if (x!=0)
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS public class IfElse { 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”); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS else-if if (ekspresi boolean1) { pernyataan1; } else if (ekspresi boolean2) { pernyataan2; } else { pernyataan3; } pernyataan4; if ekspresi2 = true pernyataan3 pernyataan4 YN pernyataan2 if ekspresi1 = true N pernyataan1 Y
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS switch switch (ekspresi) { case konstanta1 : pernyataan1; break; case konstanta1: pernyataan2; break; default : pernyataan3; } pernyataan4; if ekspresi = konstan2 pernyataan3 pernyataan4 YN pernyataan2 if ekspresi = konstan1 N pernyataan1 Y if break if break Y N Y N
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS 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.
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS 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); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS The Loop Statements The for() Loop The while() Loop The do – while() Loop
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS for for (inisialisasi; ekspresi boolean; perubah) { pernyataan; } Inisialisasi if kondisi = true pernyataan perubah Y N for (inisialisasi; kondisi; perubah) pernyataan
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS for Java programming language allows the comma separator in a for() loop structure. Example: for (i=0, j = 0; j<10; i++, j++){}
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS for for (int i=0; i<10; i++) { System.out.println(“Hore !!”); } for (int i = 0; i < 10; i++) { System.out.println("Are you finished yet?"); } System.out.println("Finally!");
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS while while (ekspresi boolean) { pernyataan; } if kondisi = true pernyataan Y N while (kondisi) pernyataan
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS while int i = 0; while (i<10) { System.out.println(“Hore !!”); i++; } int i = 0; while (i < 10) { System.out.println("Are you finished yet?"); i++; } System.out.println("Done");
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS do while do { pernyataan; } while (ekspresi boolean); if kondisi = true pernyataan Y N while (kondisi) pernyataan
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS do while int i = 0; do { System.out.println(“Hore !!”); i++; } while (i<10); int i = 0; do { System.out.println("Are you finished yet?"); i++; } while (i < 10); System.out.println("Done");
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Special Loop Control break [label]; continue [label]; label : statement; (statement ini berupa loop)
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Special Loop Control break digunakan untuk keluar (“prematurely exit”) dari switch statements, loop statements, dan labeled blocks. continue digunakan untuk meneruskan (“skip over and jump) ke akhir dari loop body, dan kembali ke loop control statement. label digunakan untuk mengidentifikasi statement lain dimana statement lain ini meminta supaya block statement pada label ini dikerjakan.
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 1 : if import java.util.Scanner; public class IfElseName { public static void main(String [] args) { Scanner input=new Scanner(System.in); char firstInisial = (char) -1; System.out.println("Enter your first inisial = "); try{ firstInisial=(char)input.next().charAt(0); } catch(Exception e) { System.out.println("Error = "+e.toString()); } if (firstInisial=='A') System.out.println("Your name must be Ali"); else if(firstInisial=='B') System.out.println("Your name must be Basuki "); else System.out.println("I can't figure your name"); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 2 : switch without break import java.util.Scanner; public class IfElseName { public static void main(String [] args) { Scanner input=new Scanner(System.in); char firstInisial = (char) -1; System.out.println("Enter your first inisial = "); try{ firstInisial=(char)input.next().charAt(0); } catch(Exception e) { System.out.println("Error = "+e.toString()); } switch(firstInisial){ case ‘A’: System.out.println("Your name must be Ali"); case ‘B’: System.out.println("Your name must be Basuki "); default : System.out.println("I can't figure your name"); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 3 : switch with break import java.util.Scanner; public class IfElseName { public static void main(String [] args) { Scanner input=new Scanner(System.in); char firstInisial = (char) -1; System.out.println("Enter your first inisial = "); try{ firstInisial=(char)input.next().charAt(0); } catch(Exception e) { System.out.println("Error = "+e.toString()); } switch(firstInisial){ case ‘A’: System.out.println("Your name must be Ali");break; case ‘B’: System.out.println("Your name must be Basuki ");break; default : System.out.println("I can't figure your name"); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 4 : if else dan for public class ForCount { public static void main(String [] args) { Scanner input=new Scanner(System.in); char nilai=(char)-1; int numToCount; System.out.println("Enter number to count between 0-10 = "); try{ nilai=input.next().charAt(0); } catch(Exception e) { System.out.println("Error = "+e.toString()); } numToCount=Character.digit(nilai, 10); if((numToCount>0)&&(numToCount<10)){ for(int i=0;i<=numToCount;i++) System.out.println(i); } else System.out.println("The number was not between 0 and 10"); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 5 : if else dan while public class ForCount { public static void main(String [] args) { Scanner input=new Scanner(System.in); char nilai=(char)-1; int numToCount; System.out.println("Enter number to count between 0-10 = "); try{ nilai=input.next().charAt(0); } catch(Exception e) { System.out.println("Error = "+e.toString()); } numToCount=Character.digit(nilai, 10); if((numToCount>0)&&(numToCount<10)){ int i=0; while (i<=numToCount){ System.out.println(i);i++; } else System.out.println("The number was not between 0 and 10"); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 6 : do while with break public class BreakLoop { public static void main(String [] args) { int i=0; do{ System.out.println("I'm stuck"); i++; if(i>9)break; }while(true); }
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 7 : switch case dan if else import java.util.Scanner; public class SwitchDemo2 { public static void main(String [] args) { Scanner input=new Scanner(System.in); int month,year,numDays=0; System.out.println("Masukkan Bulan ke (1-12) = "); month=input.nextInt(); System.out.println("Masukkan Tahun = "); year=input.nextInt(); switch(month) { case 1: case 5: case 7: case 8: case 10: case 12: numDays=31; break; case 4: case 6: case 11 : numDays=30; break;
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 7 : switch case dan if else (con’t) case 2: if(year%4==0) numDays=29; else numDays=28; break; }//end of case System.out.println("Num of Days = "+numDays); }//end of main }//end of class
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 8 : for dan array
Laboratorium Computer Vision Politeknik Elektronika Negeri Surabaya PENS-ITS Prak 9 : Looping dan if elseif Setiap matakuliah di Jurusan TI setiap akhir semester akan dianalisa berapa jumlah siswa yang mendapat grade “A”, grade “B”, grade “C”, dan grade “D” diketahui konversi nilai ke huruf sbb : A B C D <60 Berapa nilai rata-rata siswa dalam 1 kelas Sebagai input adalah : nilai siswa dalam 1 kelas dimisalkan jumlah siswa = 10 output nilai huruf A,B,C,dan D tiap siswa dan nilai rata- rata siswa dalam 1 kelas